- Home /
Smooth camera on moving platform
I have this script of camera controller which follow the player:
public GameObject player;
private float smoothness = 0.1f;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void FixedUpdate () {
Vector3 desiredPosition = player.transform.position + offset;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothness);
transform.position = smoothedPosition;
}
Normally it goes nicely and very smooth. However when the player object boards a moving platform, the camera stutters back and forth a little which cause image tearing. What should I add to make it smooth?
Here is the script to hold the player object on a moving platform which I suspect as the cause of the issue:
private Vector3 offset;
private GameObject target = null;
private void Start()
{
target = null;
}
private void OnTriggerStay(Collider other)
{
target = other.gameObject;
offset = target.transform.position - transform.position;
}
private void OnTriggerExit(Collider other)
{
target = null;
}
private void LateUpdate()
{
if(target != null)
{
target.transform.position = transform.position + offset;
}
}
Thank you in advance!
Try making the player a child of the platform when he is grounded on the platform, and of course unparent when he is not grounded
If the player object become the child of the platform, it will not be able to control since the player object is a sphere that move by force. I used the "hold" method above so the player object can move freely on the platform while sticking on it ins$$anonymous$$d of always positioned at the center of the platform.
Answer by laurG · Mar 26, 2019 at 05:14 AM
Following @highpockets 's comment,: depending on the player controller script, it should be possible to move it even if it's the child of the platform, but that's not the issue here:
FixedUpdate is for physics, so the Camera script shouldn't use it. Change it to LateUpdate and it will work.
(FixedUpdate is called at fixed intervals, independent of the framerate, so if your deltaTime is lower than the Time.fixedTimeStep (usually 0.016~0.02) it means update is called more often so Camera will "lag" behind because of the FixedUpdate being called less often than Update/LateUpdate)
I changed it to LateUpdate/Update like you said. The camera now very smooth on the platform. However the player object now stutter when moving normally (I assume it is because of being controlled by physics/force). Anyway thank you for pointing out the problem!
Never$$anonymous$$d, I just forgot the rigidbody interpolate.
Your answer
Follow this Question
Related Questions
Make a camera to Follow two players 2 Answers
Help make camera zoom smoother 3 Answers
How to make a camera Follow an Object moving in zigzag path? 1 Answer
Camera move & rotate along vehicle 0 Answers