- Home /
custom follow script choppy movement
Hi, I made a little follow script for a flying game and it works just fine, however camera movement gets rather choppy. I can lower the smooth parameter to make it less choppy, but doing so means the camera will follow way to slow. Does anyone know of a way to fix this choppyness?
Here's my current script:
#pragma strict
var player: GameObject;
var target : Transform; //this is a node behind my aircraft, not the same as player...
var oldPos: Vector3;
var newPos: Vector3;
var smooth: float = 5;
function Awake()
{
player = GameObject.FindWithTag("Player");
oldPos = transform.position;
newPos = target.position;
}
function LateUpdate()
{
oldPos = transform.position;
transform.rotation = player.transform.rotation;
transform.position = Vector3.Lerp(oldPos, newPos,Time.deltaTime * smooth);
newPos = target.position;
}
Answer by Tim-Michels · Jul 04, 2012 at 07:17 AM
You should smoothen your rotation too. The choppy effect probably comes from the fact that you're setting the rotation hard each frame.
Just use that Lerp with smoothing on your rotation too.
Example
transform.rotation = Quaternion.Lerp(transform.rotation, player.transform.rotation, Time.deltaTime * smooth);
Hmm, it seems that makes it slightly less choppy, tho it still is choppy. I'm assu$$anonymous$$g the target values of lerp are being reached before I update the positions etc. I'm going to try playing around with my smooth variable a bit more and see if that will make it more smooth without making it to slow.
EDIT: it seems that using the oldPos and newPos variables was the cause of the choppiness, using the values I assign to them directly in lerp seems to work way better.
EDIT: never$$anonymous$$d, just had my smooth value too low, was accidentally adjusting a different variable haha. still choppy D:
Answer by Tim-Michels · Jul 04, 2012 at 08:41 AM
I would suggest something else for your position smoothing...
You can also do:
transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime * smooth);
By further tweaking your smooth variable, combined with your rotation smoothing, you should get a better result.
Please let me know if that helped.
This is what I did, but I also changed newPos to target.position, as this seemed to make it a little smoother, is there any reason why you kept newPos the same, because to me using target.position seems smoother. This still is a bit choppy tho, and I can't have it follow any slower than it is right now, or the player will lose sight of the aircraft. I think I should have a way to slow down the camera when it gets close to the target position, much like quaternion.slerp for rotations
Your answer
Follow this Question
Related Questions
Modified SmoothFollow 0 Answers
Object lerping in same direction as camera jitters 0 Answers
Unsmooth vehicle-following camera 1 Answer
3rd Person Camera Problem 1 Answer
Lerping Field Of View is buggy 1 Answer