function Update () {
if (Input.GetAxis("Vertical"))
transform.rotation.y = Camera.main.transform.rotation.y;
if (Input.GetAxis("Horizontal"))
transform.rotation.y = Camera.main.transform.rotation.y;
}
this makes it so when my character moves he looks in the same direction as the camera but it only works half the time can someone please help.
I guess you have another script on your Camera that rotate the camera whenever Vertical or Horizontal is changing. If this code is also being done in Update, there’s no telling which of the two are updated first. You can use LateUpdate to change the rotation, since LateUpdate is called after all other scripts have run Update. And you probably don’t need to have that input check but rather update rotation each frame. Since it’s a quaternion, maybe we shouldn’t be messing with the components directly? We can get the same effect by taking the forward direction of the camera, ignore the y component, normalize the direction if it’s a valid direction still and finally use transform.LookAt to look in the same direction.
function LateUpdate() {
var fwd : Vector3 = Camera.main.transform.forward;
fwd.y = 0;
if (fwd.sqrMagnitude != 0.0f) {
fwd.Normalize();
transform.LookAt(transform.position + fwd);
}
}