- Home /
Smooth camera follow seems to be a bit choppy
So I wrote this custom 2D smooth camera follow script, but as soon as the camera is supposed to follow the player object the player object its self seems to jerk around. This is a visual effect and not a product of the script i'm using to actually move the character, I am sure of that.
My best guess is that something is causing the camera to interpolate between positions between frames, (I.E. the transform.position is being changed from zero to the position of the player), I cannot seem to locate the issue. So I turn to you! The script is below feel free to test it/keep it, but please help me find what is wrong.
using UnityEngine;
using System.Collections;
public class cameraMechanics : MonoBehaviour {
public float camDist, slugRate, slugFollowRate;
public Transform mainObject;
Vector3 camPos;
Transform follow;
void Start(){
camPos = transform.position;
follow = mainObject;
}
void Update(){
float dist = Vector2.Distance((Vector2)camPos, (Vector2)follow.position);
if(dist > slugRate){
Vector2 cPS = (Vector2)camPos;
cPS += ((Vector2)camPos - (Vector2)follow.position) * (slugFollowRate * dist);
camPos = (Vector3)cPS;
}
camPos.z = camDist;
transform.position = camPos;
}
}
Thank you in advance for any advice or help!
Update: The jitter problem only occurs with the object that is controlled with the character controller, essentially anything moved with regular controls.
Answer by Issah · Aug 04, 2014 at 08:18 AM
Hello,
http://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html
" For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update."
When you'r camera follow the transform.position in update, the value can be calculated at the wrong moment, between two frame like you say, try to put you'r following instruction in LateUpdate and your forces in FixedUpdate.
Hope it will help
Good suggestion, but moving the camera update to lateupdate and the controls to fixedupdate didn't seem to fix the problem.