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 stealthnachos · Sep 03, 2013 at 10:23 PM · lerptime.deltatimeorigin

Lerping using Time.deltaTime results in object going to origin.

I've been working on this all day and can simply not find what the problem is, I've looked all over unityAnswers and have not found a working solution, though I seem to be getting closer. Whenever I use a the following my object will move down to the correct position and then at the end suddenly jump to the origin. It's the most infuriating thing. I've done some prints to try to figure out what exactly happens and everything works until the very end. After that I can no longer move it down and it moves up to the wrong place. It's so nutty.

 if(Input.GetButtonDown("up") && transform.position.y < 40 && !movingUp)
     {
         var targetPosY : Vector3 = Vector3(transform.position.x, transform.position.y + 40, transform.position.z);
         movingUp = true;
         
         print("on up" + targetPosY);
     }
     
     if(movingUp && transform.position.y < targetPosY.y)
     {
         MoveObject(transform.position,targetPosY, 0.5f);
         
         if(transform.position.y >= targetPosY.y)
         {
             //transform.position.y = targetPosY.y;
             movingUp = false;
         }
         //print("upB" + movingUp);
     }
     
     
     if(Input.GetButtonDown("down") && transform.position.y > -70 && !movingDown)
     {
         //var currentNegY : Vector3= Vector3(transform.position.x, transform.position.y, transform.position.z);
         var targetNegY : Vector3 = Vector3(transform.position.x, transform.position.y - 40, transform.position.z);
         movingDown = true;
         
         print("on down" + targetNegY);
     }
     
     if(movingDown)
     {
         MoveObject(transform.position,targetNegY, 0.5f);
         //transform.Translate(Vector3.down * 1);
         
         if(transform.position.y <= targetNegY.y)
         {
             transform.position.y = targetNegY.y;
             movingDown = false;
         }
         //print("downB" + movingDown);
         print("on down" + targetNegY);
     }

 function MoveObject (startPos : Vector3, endPos : Vector3, time : float) {
 
     var i = 0.0;
     var rate = 1.0/time;
     
     while (i < 1.0) 
     {
         i += Time.deltaTime * rate;
         transform.position = Vector3.Lerp(startPos, endPos, i);
         yield; 
     }
 }

Sorry about the trashy code, I've tried so many things in an attempt to get this to work. 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
1
Best Answer

Answer by aldonaletto · Sep 04, 2013 at 01:44 AM

Well, I couldn't understand what exactly you wanna do, but suppose that you just want to move up and down in steps of 40 units. From your code, I suspect that you're starting multiple MoveObject coroutines - this probably would produce the weird behaviour you've described.

To avoid this, use a boolean flag moving : set it when MoveObject starts and clear it when the loop finishes, and only check the keyboard when moving is false:

 private var moving = false;
 
 function Update(){
   if (!moving){ // if not currently moving...
     if (Input.GetButtonDown("up") && transform.position.y < 40){
       var targetPosY : Vector3 = transform.position + 40 * Vector3.up;
       MoveObject(transform.position,targetPosY, 0.5f); // move 40 up
     }
     if (Input.GetButtonDown("down") && transform.position.y > -70){
       var targetNegY : Vector3 = transform.position - 40 * Vector3.up;
       MoveObject(transform.position,targetNegY, 0.5f); // move 40 down
     }
   }
 }
  
 function MoveObject (startPos : Vector3, endPos : Vector3, time : float){
   moving = true; // I'm moving - don't call me until the movement is done
   var i = 0.0;
   var rate = 1.0/time;
   while (i < 1.0){
     i += Time.deltaTime * rate;
     transform.position = Vector3.Lerp(startPos, endPos, i);
     yield; 
   }
   moving = false; // movement done - you can call me again
 }
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 stealthnachos · Sep 04, 2013 at 02:10 AM 0
Share

While this is 100% better written than $$anonymous$$e and I def appreciate the effort for that it's still having the same problem. This makes it move down over a short time towards the world space origin, ins$$anonymous$$d of simply negative 40 units on the Y axis, when I press the down key, in this case "s". Important to note: this doesn't happen when I make it go up. It simply goes up 40 units on the Y axis from wherever it's at the time.

avatar image stealthnachos · Sep 04, 2013 at 02:19 AM 0
Share

Okay, checked the code again and there was just a small typo which I left in there because I copy pasted the code. It works now, thank you thank you thank you. I was beating my head on this all day.

avatar image aldonaletto · Sep 04, 2013 at 03:01 AM 1
Share

Yes, there was a typo in the down if - I used targetPosY ins$$anonymous$$d of targetNegY. Answer fixed now.

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

camera Lerp Silly Question 3 Answers

When enemy health is 0, the player gets 73 XP, about 500,000 times. 2 Answers

Vector3 Lerp using Time.deltaTime? 3 Answers

Making a 2D Game, Flipping Textures ERROR 1 Answer


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