Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by h0nestjim · May 02, 2018 at 04:03 PM · movementvector3lerpcoroutines

Smooth Forward Movement with a CoRoutine

Hi All,

I have been playing around with coroutines and have smooth movement to slowly rotate a game object 90 degrees, as apposed to instantly slipping it 90 degrees on a button press.

What i have been trying to do this afternoon is slowly move a Game Object forward 5 units on it's Forward Vector, but again, this needs to be a smooth forward movement rather than an instant movement.

I have tried the below code, but it only navigates to position 5, and when I rotate it and move it forward again, it simply moves towards the NEW forward location of (0, 0, 5).

I know it has something to do with finding and Lerping to the new position but I just can't seem to set it. I am calling this from a button press if statement in void update.

     IEnumerator MoveForward() 
     {   
         float inTime = 0.8f;
 
         Vector3 fromPos = transform.position;
         Vector3 Endpos = transform.forward * 5f;
 
         for (float t = 0f; t <= 1; t += Time.deltaTime / inTime) {
 
             transform.position = Vector3.Lerp (fromPos, Endpos, t);
 
             yield return null;
         }
          }
 

Thanks in advance.

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
9

Answer by arrnav · May 09, 2020 at 09:43 AM

I know it's been 2 years and this must have been solved long ago by OP, but the answer may still come of use for beginners looking at this question. So here goes..

Don't use Update to Lerp unless absolutely necessary. If statements can make your code quickly messy. Coroutines give you way more control.

Also, the problem is at the 6th line of your code. You were using transform.forward instead of transform.position + transform.forward as your final position everytime this function is called, causing the object to go to the (0, 0, 5) position every time instead of using it's previously updated position.

What you want to do is add (transform.forward 5)* to your current position to move in that direction by 5 units correctly every time on your button press.

The following code should work, it is a simple Lerp within a Coroutine, so it should work with transform.forward with ease :



 private IEnumerator SmoothLerp (float time)
 {
         Vector3 startingPos  = transform.position;
         Vector3 finalPos = transform.position + (transform.forward * 5);

         float elapsedTime = 0;
         
         while (elapsedTime < time)
         {
             transform.position = Vector3.Lerp(startingPos, finalPos, (elapsedTime / time));
             elapsedTime += Time.deltaTime;
             yield return null;
         }
 }



Call the Coroutine using -

 StartCoroutine (SmoothLerp (3f));  // 3f is the time in seconds the movement will take.



As both Lerp & Coroutine functions are completely orthogonal, they work well together, so you don't really NEED DoTween or iTween unless working with UI elements.

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

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

Related Questions

DOTween sequence snapping 0 Answers

Vector3.Lerp not moving object backwards 0 Answers

Smoother grid based movement using coroutines and lerp 0 Answers

Rotating object with Lerp in coroutine with correct speed 0 Answers

Contrary if statements being called at the same time 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