• 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
Question by Firedan1176 · Jul 12, 2014 at 04:49 AM · vector3lerpdistance

Vector3.Lerp - Constant speed between distance changes

I'm lerping from object 1 to object 2. I want the rate at w$$anonymous$$ch object 1 travels to be the exact same, using Time.deltaTime or Time.time. So if the distance between object 1 and object 2 are 5 meters at one point, I want object 1 to travel at 1 meters per second. If they were 1,000 meters apart, I still want object 1 to travel 1 meter per second. I know the solution is right at my fingertips, but I can't figure it out. I know it's gotta do with Vector3.Distance(object1.transform.position, object2.transform.position) but I don't know what else to do!

If I have speed set defaultly at 2, what can I do to make sure that the Vector3.Lerp (obj1, obj2, Time.deltaTime * newSpeed) that the newSpeed is the correct speed (it's gotta be less than the original speed, w$$anonymous$$ch is 2 in t$$anonymous$$s case)? Thanks.

Comment
Ricna
RodrigoSeVeN
Fewpwew130
mconradie

People who like this

4 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by robertbu · Jul 12, 2014 at 05:01 AM

A solution is to use Vector3.MoveTowards() rather than Vector3.Lerp().

 transform.position = Vector3.MoveTowards(transform.position, other.position, Time.deltaTime * speed);

Speed is a variable you define and is measured in units per second. So if you set it to 1.0, your object will move at a constant 1.0 units per second no matter the distance. Note that in t$$anonymous$$s form of MoveTowards, the first parameter must be the current position of the object, not the starting position of the object.

Comment
Firedan1176
Em3rgency
nat-soragge
Maxpeinas
Reynarz
Jaazinh
Westland
nicmarxp
Ricna
RodrigoSeVeN
Fewpwew130
Filip8429
Ragee
CheMBurN
Dieter_Dagger
And 2 more...

People who like this

17 Show 2 · 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 Firedan1176 · Jul 12, 2014 at 01:11 PM 0
Share

This is exactly what I was looking for! Thank you.

avatar image aanimation · Dec 29, 2014 at 06:41 AM 0
Share

great..its working

avatar image

Answer by RodrigoSeVeN · Oct 15, 2019 at 10:17 PM

Vector3.MoveTowards() is likely the best approach, but here's the answer for Lerp:

         float distance = Vector3.Distance(object1.transform.position, object2.transform.position);
         float finalSpeed = (distance / newSpeed);
         transform.position = Vector3.Lerp(obj1, obj2, Time.deltaTime / finalSpeed);

Dividing Time.deltaTime by distance makes it move as you wanted.

Dividing distance by newSpeed changes the speed.

Now just find the best value for newSpeed (might need to increase it a little).

Comment
Ragee
DavidSof
Feelnside
sfoust

People who like this

4 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
avatar image DavidSof · Dec 18, 2021 at 09:01 PM 1
Share

oo man thank you save my day

avatar image

Answer by Masserz · Feb 03, 2015 at 03:41 AM

Thanks, I use both:

transform.position = Vector3.MoveTowards (transform.position, other.position, Time.deltaTime * speed / 2);

transform.position = Vector3.Lerp (transform.position, other.position, Time.deltaTime * speed / 2);

to balance the Lerp acceleration and linear effects. Is there a better way of doing t$$anonymous$$s?

Comment

People who like this

0 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
avatar image $$anonymous$$ · Oct 15, 2019 at 09:11 PM 0
Share

I haven't figured it quite out yet, but you would need a Mathf.Lerp function for the third MoveTowards parameter.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Find center between 4 points 3 Answers

Move Transform to Target in X seconds 3 Answers

make my gameobject lerp between points more smoothly 0 Answers

Enabling and Disabling gameobjects 1 Answer

Vector3 Lerp mis-match 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