- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
ketain · Oct 30, 2013 at 04:07 PM ·
cameralerpchildfollow path
How to lerp a child towards parent position while parent is moving
I have a camera which is a child of my playerObject. The camera is always looking at the player in a 90 degree angle and is always on the same side of the player (like in Valkyrie Profile 2). This stuff already works. The problem now is that i want the movement of the camera to be lerped when it follows the player.
Comment
Best Answer
Answer by aldonaletto · Oct 30, 2013 at 04:42 PM
Do you want to follow the player with some delay? If so, set the camera's position/rotation in LateUpdate (camera script):
public speed: float = 5.0;
private relPos: Vector3;
private target: Transform;
private tr: Transform;
function Start(){
tr = transform; // cache camera transform in tr
relPos = tr.localPosition; // get initial relative position
target = tr.parent; // our target is the current parent (the player)
tr.parent = null; // unchild the camera - it will move by itself
}
function LateUpdate(){
// find the destination position in world space:
var destPos: Vector3 = target.TransformPoint(relPos);
// lerp to it each frame:
tr.position = Vector3.Lerp(tr.position, destPos, speed * Time.deltaTime);
// keep your eyes on the target
tr.LookAt(target);
}
The camera was detached from the player just to avoid wasting time with child movement since this will be done in LateUpdate anyway. If the camera must be a child of the player for other reasons, however, just delete the last instruction in Start.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120935im_/https://answers.unity.com/themes/thub/images/avi.jpg)