- Home /
Camera Follow Code Causes Jerky Movements
I need a script that will act like the included SmoothFollow.js but will also rotate on the X and Z axes. I messed around with it and came up with this:
var target : Transform;
var distance = 5.0;
function Update () {
if (!target) {
return;
}
wantedRotationAngleY = target.eulerAngles.y;
currentRotationAngleY = transform.eulerAngles.y;
wantedRotationAngleX = target.eulerAngles.x;
currentRotationAngleX = transform.eulerAngles.x;
currentRotationAngleY = Mathf.LerpAngle(currentRotationAngleY, wantedRotationAngleY, Time.deltaTime);
currentRotationAngleX = Mathf.LerpAngle(currentRotationAngleX, wantedRotationAngleX, Time.deltaTime);
currentRotation = Quaternion.Euler(currentRotationAngleX, currentRotationAngleY, 0);
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
transform.localEulerAngles = target.transform.localEulerAngles;
}
The script shown above does its intended job perfectly however it is extremely jerky when rotating especially on X axis rotation. Is there a way to better write this script or a way to smooth it? Once this is fixed I will be applying dampening so making the camera a child is not an option. Thank you.
You need to switch the movement part and the camera follow areas of the code. You must move the player then make the camera follow. That will eli$$anonymous$$ate the jerky movement
I switched the transform.localEulerAngles and the transform.position in code so the angle changes before the position since that's all I can assume you mean and that fixed everything. Thanks! That comment is an accepted answer.
Answer by EnglishMuffin123 · Dec 01, 2013 at 08:16 AM
To anyone looking for a script like this, MrProffesorTroll has fixed it. Please read the comments under the question for an explanation but here is the completed code without dampening added:
var target : Transform;
var distance = 5.0;
function FixedUpdate () {
if (!target) {
return;
}
wantedRotationAngleY = target.eulerAngles.y;
currentRotationAngleY = transform.eulerAngles.y;
wantedRotationAngleX = target.eulerAngles.x;
currentRotationAngleX = transform.eulerAngles.x;
currentRotationAngleY = Mathf.LerpAngle(currentRotationAngleY, wantedRotationAngleY, Time.deltaTime);
currentRotationAngleX = Mathf.LerpAngle(currentRotationAngleX, wantedRotationAngleX, Time.deltaTime);
currentRotation = Quaternion.Euler(currentRotationAngleX, currentRotationAngleY, 0);
transform.localEulerAngles = target.transform.localEulerAngles;
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
}
Answer by Dean432 · Dec 07, 2013 at 12:40 AM
I don't know if anyone has noticed this but if you get jerky movements with the follow transform script from the mobile assets, i found a way to stop it jerking but its not a fix, i was fiddling around trying to find out what made the character so jerky and found that is was the follow transform script on the camera pivot, though if you run the game and turn the script off and on again in the inspector while the game is still running, it thens runs perfectly smooth like in previous versions of unity, if this helps anyone with fixing the script that would be great.
Your answer
Follow this Question
Related Questions
How to make the camera rotate only when right click is held down on the mouse? 1 Answer
character view range 2 Answers
Unity automatically rotate camera on slopes up/down 0 Answers
Player making a 360° looping, camera not following 0 Answers
How would you set a camera lerp position with code not a child object? 1 Answer