- Home /
Constant Transform Lerp
Is it bad practice to let a transform constantly re-position and rotate itself to a target with Vector3.Lerp and Quaternion.Lerp? For example:
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, head.position, cameraSlerpPositionSpeed * Time.deltaTime);
Camera.main.transform.rotation = Quaternion.Lerp(Camera.main.transform.rotation, head.rotation, cameraSlerpRotationSpeed * Time.deltaTime);
Is this valid or is it something a programmer should avoid?
Answer by rutter · Sep 09, 2014 at 06:58 PM
In many cases, all a game does is reposition and rotate things. :) Go ahead and do so as much as you like.
However, many people misunderstand what lerp does. Given two values, start
and end
, a lerp returns another value that is X% between them.
In your example code, if cameraSlerpPositionSpeed
is 1 and your game runs at 50 FPS, you'll generally move the camera 2% closer to its target each frame. If cameraSlerpPositionSpeed
is 40, you'll move it 80% closer each frame.
Now, you can call lerps with a fixed value to create a sort of "easing" effect. If your object moves 50% closer each frame, it will start out moving very quickly, and will slow down as it nears its target... but it won't necessarily ever reach that target.
Your variable naming and function params give me a feeling that you're not trying to do that, though.
If you expect a rate like "camera moves X meters per frame" or "camera rotates Y degrees per frame", you either need to set up your lerp calls with a timer, or use helper functions like Vector3.MoveTowards and Quaternion.RotateTowards:
transform.position = Vector3.MoveTowards(transform.position, goalPosition, metersPerSecond * Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, goalRotation, degreesPerSecond * Time.deltaTime);
Thanks alot rutter, what I'm trying to do is to switch between first person views of multiple characters on mouse click. I tried a more complicated way with lerp and parenting the camera to the head bone to prevent a performance hit, but now after reading the answers to my question, I am glad a single lerping camera will be enough.
Answer by KMKxJOEY1 · Sep 09, 2014 at 06:57 PM
perfectly valid; it does use some computational expensive methods but unless there is a whole lot of objects using these methods then you should be good :) Always a plus when programmers are thinking about performance though!
Thanks for the confirmation! Luckily I just need to move the main camera around the scene that way.
Answer by ThePersister · Sep 09, 2014 at 07:00 PM
That's perfectly fine for as far as I know, just be careful with using .transform in Updates. You probably know GetComponent() is heavier on the system than things with variables, right?
Well, .transform is also actually a GetComponent() in disguise. So you should consider storing the transform in a variable and then using that. Which ends up in:
Transform mCameraMainTrans;
void Start() {
mCameraMainTrans = Camera.main.transform;
}
void Update() {
mCameraMainTrans.position = Vector3.Lerp(mCameraMainTrans.position, head.position, cameraSlerpPositionSpeed * Time.deltaTime);
mCameraMainTrans.rotation = Quaternion.Lerp(mCameraMainTrans.rotation, head.rotation, cameraSlerpRotationSpeed * Time.deltaTime);
}
So yea, for as far as I know it's pretty valid. However I'd be englightened if told otherwise by a more experienced Unity Programmer, so I'm only 90% sure :)
Also, I've surely seen algorithmic structures that go far far far beyond this and still work out perfectly fine, so you should be fine either way ;)
If you like my answer enough, you can accept it, I'd appreciate it :)
Thanks for the advice, I sometimes find it kind of enoying to do it for every component in the script, but you are right. The advantage is obvious. :)