Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 J-F · Jun 01, 2020 at 01:23 PM · ienumeratorsnapvector3.lerp

Vector3.Lerp snaps when called too frequently.

Hey.

I have a problem with my code where the Vector3.Lerp method snaps into it's target position instead of interpolating to it properly when called too frequently. Here is the class handling this:

 public class c_toggleCamera : MonoBehaviour
 {
     public Transform[] tgt;
     public int index;
 
     public float speed = 1.0F;
 
 
     void Start()
     {
         transform.position = tgt[0].position;
         transform.rotation = tgt[0].rotation;
     }
 
     public IEnumerator MoveToSpot(int i)
     {
         float elapsedTime = 0;
 
         Transform currentPos = transform;
         Transform targetPos = tgt[i];
 
         while (elapsedTime < speed)
         {
             transform.position = Vector3.Lerp(currentPos.position, targetPos.position, (elapsedTime / speed));
             transform.rotation = Quaternion.Lerp(currentPos.rotation, targetPos.rotation, (elapsedTime / speed));
             elapsedTime += speed*Time.deltaTime;
             yield return null;
         }
         transform.position = targetPos.position;
         transform.rotation = targetPos.rotation;
         index = i;
         yield return null;
     }
 }

How could i smooth this out?

Comment
Add comment · Show 2
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 KoenigX3 · Jun 01, 2020 at 01:31 PM 0
Share

Have you tried altering the float variables? Because in this case it seems to me that the interpolation is happening in 1 frame. ElapsedTime / speed will be more than 1 after the first frame. Try this:

 float length = 300;
 float speed = 1;
 
 public IEnumerator $$anonymous$$oveToSpot(int i)
 {
    while(elapsedTime < length)
    {
       transform.position = Vector3.Lerp(currentPos.position, targetPos.position, elapsedTime / length);
       // Same for the rotation
 
       elapsedTime += speed * Time.deltaTime;
       yield return null;
    }
 }
avatar image J-F · Jun 01, 2020 at 04:42 PM 0
Share

No, that's not my problem. It interpolates fine with the code i provided. $$anonymous$$y issue occurs when i try to call $$anonymous$$oveToSpot(x) again while it's still interpolating to another destination. This causes it to either snap to it's new destination without any interpolation, or "bounce" in a strange way before getting to it's destination. On some occasions, both of the side effects mentioned above occur together.

I tried your code, but it slows down to a halt before reaching it's destination.

2 Replies

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

Answer by tadadosi · Jun 01, 2020 at 04:53 PM

My guess is that the coroutines are overlapping and that you should probably stop the current routine before starting MoveToSpot(x) again. Unity Docs StopCoroutine

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

Answer by Captain_Pineapple · Jun 01, 2020 at 05:04 PM

Your problem is that you do not save a position (which would be a copy by value) but instead save a reference to a transform. In the end this means that currentPos.position does not really resemble the starting point but instead always is the actual current position of the object you are moving.


So your problem should be solved if you just change the currentPos from beeing a Transform to a Vector3 currentPos = transform.position;


same should probably be done for the target transform as well.

this will then result in actual linear movement. You still probably have to reduce your speed though.

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

134 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

Related Questions

Run Coroutine once, but finish Lerping 2 Answers

Problem using Arrayed Transforms in IEnumerator 0 Answers

How to move GameObject after 1 second c# 2 Answers

Drag n Drop Cube To Snap In Place - Grid 1 Answer

Is there a way to check if something was snapped on a specific grid? 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