I’m experiencing an issue where a game object appears to jitter while rotating at constant speed when the camera follows the object. This jitter is only present when updating the object’s rotation in FixedUpdate(); moving the rotation code to Update() eliminates the issue entirely.
Here are links to GIFs of the results: (The white rectangle in the GIFs helps make the jitter more pronounced)
Rotation code in FixedUpdate(), which looks incorrect: Imgur: The magic of the Internet
Rotation code in **Update()**, which looks correct: https://imgur.com/a/aAjthb3
Why is this jitter present? Unity convention is to put this code into FixedUpdate(), because it deals with physics calculations, so I’m seeking an understanding of why I am not able to perform this behavior in FixedUpdate(). Also in keeping with convention, the camera following code is in LateUpdate(), though changing this does not seem to have an effect.
I’ve tried too many different approaches to enumerate them, but I can’t resolve this issue without using Update(). I’m really interested to know why this doesn’t work as expected. Below I will attach the only two scripts in the project in their entirety. One is for the camera following behavior, and the other is for the character rotation.
PlayerRotation.cs
using UnityEngine;
public class PlayerRotation : MonoBehaviour {
// Use this script by attaching it to a gameobject
// that game object will rotate clockwise around the up axis
public float spin_speed = 1.0f;
void FixedUpdate () {
Vector3 current_facing = this.transform.forward;
Vector3 relative_target = this.transform.right;
// pick a new direction to face that is some of the way to the right
Vector3 new_facing =
Vector3.Lerp(
current_facing,
relative_target,
spin_speed * Time.fixedDeltaTime
);
// set our direction to to the new facing
this.transform.forward = new_facing;
}
}
CameraFollower.cs
using UnityEngine;
public class CameraFollower : MonoBehaviour {
// Use this script by attaching it to a gameobject with a camera
// and supplying it with a target gameobject to follow
public GameObject target;
public float target_facing_smoothing_factor = 0.84f;
private GameObject ghost_container;
void Start () {
// create a container to be centered on the target
// make this camera a child of that container
// rotating the container will maintain the camera's
// relative offset and facing
ghost_container = new GameObject(this.gameObject.name + " (container)");
ghost_container.transform.parent = this.transform.parent;
this.transform.parent = ghost_container.transform;
}
void LateUpdate () {
// move the camera container to the target's location
ghost_container.transform.position = target.transform.position;
Vector3 current_facing = ghost_container.transform.forward;
Vector3 target_facing = target.transform.forward;
// rotate (some of the way) to the target's facing direction
ghost_container.transform.forward =
Vector3.Slerp(
current_facing,
target_facing,
target_facing_smoothing_factor
);
}
}