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 /
avatar image
2
Question by alqaadi · Jan 02, 2016 at 06:43 AM · lerplagmovinggameobject

lerp from a to moving position

lerp usually works for me but it seem it has lag when TO position is moving object, like ball catching another ball which is moving too.

so is there a way to modify this function to make it work better ,even though i don't know much of how it work.

 IEnumerator MoveFromTo(Vector3 pointA, Vector3 pointB,float time) 
     {
         if (!moving) {                     // Do nothing if already moving
             tT=Time.time;
             moving = true;                 // Set flag to true
             float tP = 0f;
             while (tP < 1.0f) {
                 tP += Time.deltaTime / time; // Sweeps from 0 to 1 in time seconds
                 transform.position = Vector3.Lerp(pointA, pointB, tP); // Set position proportional to t
                 yield return null;         // Leave the routine and return here in the next frame
             }
             moving = false;             // Finished moving
             Debug.Log("done");
             tT=Time.time-tT;
         }
     }
Comment
Add comment · Show 6
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 alqaadi · Jan 07, 2016 at 09:45 AM 0
Share

any one i'm stuck with unworkable/laggable code

avatar image Bonfire-Boy · Jan 07, 2016 at 10:20 AM 0
Share

I'm not sure I fully understand the issue, but the bottom line is that if you want it to follow a moving object you need to keep on using the position of that object. Whereas, it looks to me like this coroutine moves to a fixed position (point B).

Presumably you're passing in the position of your target as point B. But if (and as soon as) the target moves, while the coroutine is running, then it's no longer at the position to coroutine is moving to. What you're doing is moving to the position the target was at when you started moving. Is that what you mean by "lag"?

The coroutine needs to know what object its following, so that every time it yields it can check the position of that object again.

But then you'll have other issues. , Such as you'll probably want to use $$anonymous$$oveTowards to move from the current position towards the target, rather than Lerping between a fixed position and a moving one. And do you really want to do the movement in a fixed amount of time? (this won't result in smooth movement, when the target is moving).

avatar image alqaadi · Jan 09, 2016 at 10:56 AM 0
Share

well what if i know it's speed can I make it smooth

avatar image Bonfire-Boy alqaadi · Jan 09, 2016 at 06:55 PM 0
Share

Yes, if you know or can work out where the target is going to be in X seconds, then you can make the other object move to that position during that time. But that's not the same as following the target.

To properly follow the target, you could make it move towards the target on each update, by a distance that's some multiple of Time.deltaTime. But then the speed will be constant rather than how long it takes to get there.

avatar image alqaadi alqaadi · Jan 12, 2016 at 11:30 AM 0
Share

thank i will try one more question, how can i break the while loop from outside of the function

avatar image Tuncer alqaadi · Jan 12, 2016 at 11:34 AM 0
Share

put another bool outside of the function to check whether it needs to break,

 while (tP < 1.0f && !isBreak)

change value of isBreak from outside of the function

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

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

Related Questions

rigidbody.velocity vs lerp efficiency 0 Answers

Moving an object constantly between two points with a time delay inbetween 2 Answers

Moving Maincamera slowly to another position 0 Answers

How to switch 2 objects' positions gradually? 1 Answer

Scale Object Using Lerp? 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