- Home /
Smooth camera movement between two targets.
Rather than, without a transition, switching from one camera to another one, I want the camera to move from the player to the target. Since I want to make it move smoothly (slow, than fast, then slowing down again), I assume this uses "lerp". Unfortunately I'm a complete newbie at this stuff. Help, please.
Also, the camera is currently in a first-person perspective with the player. It will be togglable to third-person later, but I could probably alter the script to my liking around then.
Answer by anomalous_underdog · Feb 26, 2011 at 08:54 AM
You'll have to use one camera only, that lerps between one point to another. You didn't give any code as to what first person view script you are using so I'm not sure how your code works, but generally you can store the Transform of the target, then make your camera lerp to the position of that target.
Here is some generic code to make a Vector3 lerp transition (C#). You'll want to put this in a script for your camera:
public float transitionDuration = 2.5f; public Transform target; IEnumerator Transition() { float t = 0.0f; Vector3 startingPos = transform.position; while (t < 1.0f) { t += Time.deltaTime * (Time.timeScale/transitionDuration);
transform.position = Vector3.Lerp(startingPos, target.position, t);
yield return 0;
}
}
Assign a value to target by creating an empty GameObject, putting it at the place you want, then drag it to the target variable. You can initiate a transition by using:
StartCoroutine(Transition());
anywhere in the same file where the transition code is in.
All I'm using for the camera is the default "$$anonymous$$ouseLook" script. Also, I'd like to stick with javascript, unless it happens to be remarkably similar to C#. Sorry.
You can study the code then create the Javascript equivalent. I think in Javascript you don't need to put IEnumerator, for starters.
Learn how to program.
Really good piece of code >anomalous. 10 $$anonymous$$ute to get what i needed:)
Works perfectly, thank you so much. I tried to change too much, messed it all up, then put this back like this and it fixed everything.
Answer by seansteezy · Aug 13, 2014 at 03:16 PM
For anyone else that wants a more specialized lerp function, i added my rendition, totally based off of the great work by @anomalous_underdog. Thanks dude! This is used to move the camera to a specific point and the relative speed thing are the two bounds in which the camera can move between.
That way you can use it like this (where camPanDuration is either a variable or you can put in numbers on the fly, farLeft.position is where i want to lerp to and true just uses relative time). It's pretty specific to moving something between two points.
public void resetCam()
{
StartCoroutine(LerpToPosition(camPanDuration, farLeft.position, true));
}
IEnumerator LerpToPosition(float lerpSpeed, Vector3 newPosition, bool useRelativeSpeed = false)
{
if (useRelativeSpeed)
{
float totalDistance = farRight.position.x - farLeft.position.x;
float diff = transform.position.x - farLeft.position.x;
float multiplier = diff / totalDistance;
lerpSpeed *= multiplier;
}
float t = 0.0f;
Vector3 startingPos = transform.position;
while (t < 1.0f)
{
t += Time.deltaTime * (Time.timeScale / lerpSpeed);
transform.position = Vector3.Lerp(startingPos, newPosition, t);
yield return 0;
}
}
Hope that helps someone!
Answer by tistik · Feb 28, 2012 at 07:03 PM
I am implementing a similar behaviour at which I have added a reorientation of the camera towards the target. It works, however when the target object is behind the camera, the camera goes down in a weird move. I would like to prevent it from going too much towards the ground during the travelling (that is to say that the angle between the camera's forward and the ground should never be greater than 80°).
Does anyone have an idea on how to implement such a behaviour ?
Thanks in advance,
Your answer
Follow this Question
Related Questions
How you change another script value smoothly on trigger 0 Answers
Help with Lerp 0 Answers
Moving an object smoothly with a set distance and time 1 Answer
How to smooth my camera (Vector3.Lerp) 0 Answers
How do I make this lerp? 1 Answer