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?

In many cases, all a game does is reposition and rotate things. :slight_smile: 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);

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 :slight_smile: Always a plus when programmers are thinking about performance though!

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 :slight_smile:

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 :wink:

If you like my answer enough, you can accept it, I’d appreciate it :slight_smile: