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
0
Question by gulty · Jan 17, 2013 at 05:24 PM · buglerpmovetowards

Lerp bugging for- and backwards

Hey, I'm creating a little adventure and I have a problem with the Lerp/MoveTowards func. I have a slap function which moves an object to its direction witch a changed z coordinate. Afterwards its calling a revokeSlap func which is moving the object back to its place. Ill paste you some codesnippets: On mouseclick i set: slap = true; inside the update func.

     if (slap){
         Slap();
     }
 
     if (!wait4Lerp && revoke){
         RevokeSlap();
     }

is also part of the update func

Now the functions itself:

 function Slap(){
     wait4Lerp = true;    
     startPos = OBJECT.transform.localPosition;
     endPos = OBJECT.transform.localPosition;
     endPos.z = 0;
     OBJECT.transform.localPosition = Vector3.Lerp(
         startPos, 
         endPos, 
         Time.deltaTime*10
     );
     if(OBJECT.transform.localPosition == endPos) {
         wait4Lerp = false;
         if (!Overlap()){
             slap = false;
             revoke = true;
         }
     }        
 }

Overlap is nothing interesting here.. This works fine. Moving OBJECT to its current position with the z=0 value. If the position is reached I disable slap, wait4Lerp and set revoke to true to activate the function which returns to the original position:

 function RevokeSlap(){
     startPos = OBJECT.transform.localPosition;
     endPos = OBJECT.transform.localPosition;
         endPos.z = 10;
     var test:float = OBJECT.transform.localPosition.z;
     Debug.Log(test);
     OBJECT.transform.localPosition = Vector3.Lerp(
         startPos,
         endPos,  
         Time.deltaTime*10
     );
     //Debug.Log(OBJECT.transform.localPosition.z);
     if(OBJECT.transform.localPosition == endPos) {
         revoke = false;
         revokeComplete = true;
     };
 
 }

Now the Problem is that it mostly never reaches the endPos. It starts but in the middle it's simply going for- and backwards very very quickly and in a very small range. It's calling the RevokeSlap(); function over and over but never reaching the endPos. Sometimes after a few seconds of waiting it jumps to it and its working but still its just luck.. What am I doing wrong? Same problem with moveTowards.. Hope you can help me further..

Greets

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AeonIxion · Jan 18, 2013 at 09:44 AM

The endpos is only reached if the 3rd parameter of Vector3.Lerp is 1. If it's lower than 1 it will keep getting closer to the endpos, but never reach it. Unless the difference is so little that it will round to the endpos.

Instead of comparing the localposition and endpos, you could check if the distance of the two z's is less than 0.1 for example.

Comment
Add comment · Show 3 · 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 gulty · Jan 18, 2013 at 10:27 AM 0
Share

Well, if I use either Slap() or RevokeSlap() alone it is always working perfect. Time.deltaTime*10 is always below 1 which is giving me a smooth animation to the endpoint until it reaches it. For Slap() endPoint.z = 0, for revokeslap() endPoint.z = 10. Now tell me how should I change my func to reach that point? If I use if(endPos.z - OBJECT.transform.localPosition.z < 0.1) it will never give me z=10 but 9,xx which I don't want.. I want a simple moveanimation from x,y,10 to x,y,0 and back to x,y,10 afterwards.. Also: "you could check if the distance of the two z's is less than 0.1" <--- The distance never is less than 0.1. In my program Slap() works fine and after z=0 its going to like 3.29 -> 3.28 -> 3.29 -> 3.28 etc looping.. and sometimes its jumping to 10 after some time, sometimes not..

avatar image gulty · Jan 18, 2013 at 10:40 AM 0
Share

If I use "1" ins$$anonymous$$d of Time.deltaTime*10 it is also jumping back and forward but in revokeClap() but reaching 10 at some point.

avatar image gulty · Jan 18, 2013 at 11:13 AM 0
Share

Another interesting debug: I used a debug on OBJECT.transform.localPosition.z after entering Slap(), RevokeSlap() and after the lerp has ended. Output for Slap(): Run1: 10 -> 9.17165 Run2: 9.17165 -> 8.411917 Run2: 8.411917 -> 7.715116

As you can see the z after the Lerp is the same as the initial of the next run which is good. Now RevokeLerp(): Run1 0.09610127 -> 1.737771 Run2 1.442329 -> 3.068373 Run3 2.443556 -> 4.257556 Run4 3.436008 -> 4.524055

As you can see the value after Lerp doesnt match the value at the beginning of the next run which leads to a misscalculation of the whole function..

avatar image
0

Answer by gulty · Jan 18, 2013 at 06:27 PM

Okay, I found a solution myself. yield WaitForEndOfFrame; did the trick. I noticed that it is setting slap=false and revoke = true more than once because the animation wasn't yet finished. Therefor my if statement seemed to be setting variables even tho z wasn't really 0. On the otherside the other lerp function already startet so they kinda overlapped.

Comment
Add comment · 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

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Parsing Error CS8025 1 Answer

Creating Projectile - MoveTo/Lerp 0 Answers

Unity Gllitching Everything? 1 Answer

Unity bug when switch the platform android to ios 0 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