- Home /
(Vector3.Lerp) Camera doesn't reach the specified Destination!
Hello, Everyone
First of all, i wanna say that i have been working on it for more than 13 hours and still can't figure out what exactly the problem is, while the camera is following the player, i want the camera to go closer to the player using at certain time, so i i did use Vector3.Lerp to make the transition smooth, but the problem is that the camera doesn't fully reach the specified destination, and Here is what i mean:
The player position is (1, 1.48, 1) ,The camera position is (0.8772718, 7.560942, -2.698717)
at certain time i want the camera to move on Y axis from 7.450942 to 5.965189, but the camera doesn't reach 5.965189, it only moves from 7.450942 to 6.591753, but i want it to reach the end which is 5.965189. Also it happens with the next destination which is from 5.965189 to 3.070637, the camera doesn't reach 3.070637.
And here is my code:
public GameObject player;
public float speed = 1.0F;
float gameTime = 0.0f;
private Vector3 A = new Vector3(0.8772718F,5.965189F,-2.698717F);
private Vector3 B = new Vector3(0.8772718F,3.070637F,-2.698717F);
private Vector3 camNextPosition1;
private Vector3 camNextPosition2;
private Vector3 camCurrentPosition;
void Start()
{
camCurrentPosition = transform.position - player.transform.position;
camNextPosition1 = A - player.transform.position;
camNextPosition2 = B - player.transform.position;
}
void Update()
{
gameTime += Time.deltaTime;
Debug.Log(gameTime);
if( gameTime >= 0.0F)
{
transform.position = Vector3.Lerp(transform.position, player.transform.position + camCurrentPosition, Time.deltaTime * speed);
}
if ( gameTime >= 10.0F)
{
transform.position = Vector3.Lerp(transform.position, player.transform.position + camNextPosition1, Time.deltaTime * speed);
}
if ( gameTime >= 20.0F)
{
transform.position = Vector3.Lerp(transform.position, player.transform.position + camNextPosition2, Time.deltaTime * speed);
}
}
I think it is not reaching the destination because there is no enough time, i mean Vector3.Lerp clamped it to the range (0 to 1), but i'm not sure how to fix it if it is really the problem.
Please help me to fix this problem, i got insane because of it!
Just kidding but i may get insane if i don't get fix to it :).
Thank you all for your time, any help is greatly appreciated.
Answer by pacific00 · Aug 13, 2014 at 09:48 AM
here the problem is with the if conditions ..
if i'm understanding it correctly the first if statement should be..
if( gameTime >=0 && gameTime <= 10) { }
and similiarly the others..
I think this is along the right lines, perhaps a neater way would be use else if statements ins$$anonymous$$d of the double up on the inequalities. As well as this it would be more rigorous to not increment the lerps using Time.deltaTime but rather use your gameTime variable:
if( gameTime < 10.0F){
transform.position = Vector3.Lerp(transform.position, player.transform.position + camCurrentPosition, gameTime/10.0F);
}else if ( gameTime < 20.0F){
transform.position = Vector3.Lerp(transform.position, player.transform.position + camNextPosition1, (gameTime-10.0F)/10.0F);
}else if ( gameTime < 30.0F){
transform.position = Vector3.Lerp(transform.position, player.transform.position + camNextPosition2, (gameTime-20.0F)/10.0F);
}
Scribe
pacific00 and Scribe, your fix works great :D
i did try both...
the reason why i didn't use (else if) is that when i was using it on (Unity 4.3) only the first (else if) is executed but the rest not working! But now on (version 4.5) it works fine!
but i have some points that i want to understand if you don't $$anonymous$$d
First point, the camera doesn't stop at the exact number i mean for example the vector A, i did set the target to 5.965189 but the camera stops at 5.799795 similarly the vector B, it stops at 2.905243 but the target is 3.070637
Second point, can you please explain why you divided the gameTime by 10 and then in the first else if statments you subtracted the gameTime from 10 then divided again by 10, and so on...
Last point, the player is lagging a little while moving, but after 30 sec, the lag goes away!
Note that the camera leaves the player after 30 sec, but i did fix it by ( gameTime < 30.0F || gameTime >= 30.0F), just saying that for the benefit of others.
Thank you for your time.
so regarding your first point, perhaps changing the inequality sign I suggested from < to
second, you need to understand that the lerp function works as a ratio, if the 3rd argument (normally time) is 0 you will get your start point, if it is 1 you will get your end point, if it is 0.9 you will get 90% of the way to your end point etc... therefore having gameTime/10 means that when gameTime = 0, gameTime/10 = 0 and when gameTime = 10, gameTime/10 = 1 and so it will have moved from the start position to the end position.
Similarly when you reach gameTime = 10, you want your second lerp to be given a t value of 0, hence gameTime-10 = 0 when gameTime = 10, and (gameTime-10)/10 = 1 when gameTime = 20!
Is this script attached to your player or to the camera? I cannot see anything in this script that would cause lag, it is not very computationally heavy. Is the code in the Update method, is the player controlled by physics (rigidbody)?
Also ins$$anonymous$$d of your last inequality, if you want it to stay on the player you could just get rid of the if else and simply have an else seeing as: gameTime < 30.0F || gameTime >= 30.0F is the same as saying if less than 30 or more than 30 or equal to 30... which will always return true.
if( gameTime <= 10.0F){
transform.position = Vector3.Lerp(transform.position, player.transform.position + camCurrentPosition, gameTime/10.0F);
}else if ( gameTime <= 20.0F){
transform.position = Vector3.Lerp(transform.position, player.transform.position + camNextPosition1, (gameTime-10.0F)/10.0F);
}else{
transform.position = Vector3.Lerp(transform.position, player.transform.position + camNextPosition2, (gameTime-20.0F)/10.0F);
}
for actually trying to understand ;)
Scribe
Thanx a lot Scribe for your time, i really appreciate your help, your explanation is really nice, now i understand what was behind :)
Regarding to the script, it is attached to the camera, and yea, the player has rigidbody. Also, the code is in Update method...
No problem,
so if you disable this script on the camera the player moves fine? Is the camera a parent or child of the player object? I'm afraid I'm unlikely to be able to debug the problem without knowing the setup!
Your answer
Follow this Question
Related Questions
How to smooth my camera (Vector3.Lerp) 0 Answers
Making Camera Lerp back to original position 1 Answer
Zooming camera using move z position. 0 Answers
Camera Lerp from A to B, and back 2 Answers
Need to flatten a Vector3 2 Answers