Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Databases · Aug 13, 2014 at 09:36 AM · cameravector3lerp

(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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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..

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scribe · Aug 13, 2014 at 10:31 AM 1
Share

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

avatar image Databases · Aug 13, 2014 at 11:23 AM 0
Share

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.

avatar image Scribe · Aug 13, 2014 at 06:25 PM 1
Share

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

avatar image Databases · Aug 14, 2014 at 02:44 PM 0
Share

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...

avatar image Scribe · Aug 14, 2014 at 06:15 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges