Reputation: 153
I'm reviewing this piece of code about camera movement:
using UnityEngine;
using System.Collections;
public class CamMove : MonoBehaviour {
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity=5.0f;
public float smoothing = 2.0f;
GameObject character;
void Start()
{
//moving left and right
character = this.transform.parent.gameObject;
}
void Update()
{
var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y")); //mouse movement
md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
if(mouseLook.y>-40 && mouseLook.y<60)
transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
character.GetComponent<CharacterController>().transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);
}
}
how does he get every new locatiion ? With Math.Lerp interpolation ? Also I can't understand the part md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
Also the part :
if(mouseLook.y>-40 && mouseLook.y<60)
transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
Upvotes: 0
Views: 2978
Reputation: 90862
Well
var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y")); //mouse movement
is even commented mouse movement
. As usual a better variable name would already explain it. It would be better called mouseDelta
. So the code not using a fixed mouse position but rather the traveled dinstance since last frame. (See Input.GetRawAxis)
Then
md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
Vector2.Scale
is scaling up or down this Vector. You could also write it as
md = new Vector2 (md.x * sensitivity * smoothing, md.y * sensitivity * smoothing);
Actually it is quite unnecessary because you could write it way simplier like this:
md *= sensitivity * smoothing;
Then Mathf.Lerp
is a linear interpolation between the two given positions using a certain factor
between 0
and 1
, where 0
would be fully the first parameter, 1
fully the second, otherwise anything in between. E.g. a factor 0.5
would result in the center between both values so this depends on your given value for smoothing
smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
Again this is written quite unnecessarily because it is better written directly using Vector2.Lerp
smoothV = Vector2.Lerp(smoothV, md, 1f/smoothing);
But actually I'm not quite sure this even does what you would expect since this is no absolute value but something you later on add every frame so using a Lerp
on it makes not much sense to me anyway... I would probably directly use Lerp
for moving the current mouseLoock
towards the new target value.
Finally you do
mouseLook += smoothV;
if(mouseLook.y>-40 && mouseLook.y<60)
transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
character.GetComponent<CharacterController>().transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);
Which updates two rotations. There is nothing in your code assigning any new position at all ...
Besides that the frame wise usage of GetComponent
is extremely inefficient, you should rather store that reference once (e.g. in Start
) and reuse it later.
Upvotes: 1