- Home /
Switch Smooth Follow between targets smoothly
I have a Smooth Follow script on my main camera. This works fine. Now, I would like to switch between two targets and have the camera move smoothly between the two target and then resume following the second target. How might I accomplish this? I have tried Lerping between the two and that did not seem to work but I may just be doing it wrong. Thanks...
I don't think it did anything. I took that code out though so I can try it now.
what smooth follow script are you using, and is it a 3D or 2D game?
Using the Smooth Follow script that comes with Unity 3.5 and it is 3D.
Answer by robertbu · Mar 02, 2013 at 08:09 PM
I'm not happy with the code below, but it is all I have time for at the moment. It's based on the standard Smooth Follow. You need to Lerp() both the position and the LookAt rotation in order to get the smooth movement you want. Of course this will impact the general movement. Try it an let me know. There is another way to approach the problem (that does not involve modifying SmoothFollow).
var target : Transform;
var distance = 10.0;
var height = 5.0;
var rotationDamping = 3.0;
var movementDamping = 1.0;
function LateUpdate () {
if (!target)
return;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
currentRotationAngle = target.eulerAngles.y;
var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
var pos = target.position;
pos -= currentRotation * Vector3.forward * distance;
pos.y = wantedHeight;
transform.position = Vector3.Lerp(transform.position, pos, movementDamping * Time.deltaTime);
var q = Quaternion.LookRotation(target.transform.position - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, q, rotationDamping * Time.deltaTime);
}
That is EXACTLY what I needed! Thank you so much. I think when I tried it myself I wasn't doing the LookRotation Lerp. This works great!
Your answer
Follow this Question
Related Questions
The problem with the touch-control orbit view in the scene 0 Answers
Mouse orbit + Smooth follow script question 0 Answers
Improve smooth 2d side scroller camera to look more fluent 1 Answer
how can i make the main cam smooth follow a new prefab that i add after the game start . 5 Answers
How to make camera position relative to a specific target. 1 Answer