Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
1
Question by SoBiT · Feb 07, 2013 at 11:44 AM · objectdistancemovelookatdecrease

Decrease distance between 2 objects

How can I decrease the distance of 2 objects? Only one of the objects has to be moved, the other one kepps its position.

If this helps: The object which moves always looks at the not-moving object.

Maybe I can simply change the X and Z coords. But I've no idea how..

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

3 Replies

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

Answer by Screenhog · Feb 07, 2013 at 07:43 PM

For moving a fixed distance, there's also Vector3.MoveTowards. While it has some drawbacks compared to Lerp (wanting to smooth the animations based on a curve, for instance), I find it to be the simplest way to move one object at a constant rate towards a second point.

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
3

Answer by robertbu · Feb 07, 2013 at 12:00 PM

if Pos1 and pos2 are Vector3's representing the position of the two objects. You can use Lerp():

 Pos2 = Vector3.Lerp(Pos1, Pos2, 0.75);  

This will move Pos2 1/4 closer to Pos1. You can also do the calculation yourself (untested):

 Pos2 = Pos1 + (Pos2 - Pos1) * .75;
Comment
Add comment · Show 8 · 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 SoBiT · Feb 07, 2013 at 12:02 PM 0
Share

I'll try this. Feedback comes soon

avatar image SoBiT · Feb 07, 2013 at 12:07 PM 0
Share

This worked fine. Can I also move it a fixed distance? The function will not be called only once and then the moving distance is at the beginning maybe 500 and the last time only 30.

avatar image robertbu · Feb 07, 2013 at 12:18 PM 3
Share

You can move them a fixed distance by calculating the fraction. You can get the distance between two points are apart by:

 distApart = (pos2 - pos1).magnitude;

If you want to move pos2 by a fixed distance (delta) you can do:

 fraction = (distApart + delta) / distapart;
 Pos2 = Pos1 + (Pos2 - Pos1) * fraction;

Negative deltas move the objects closer together.

avatar image the_Whizzkid robertbu · Feb 01, 2021 at 12:55 PM 0
Share

Thank you Very $$anonymous$$uch for sharing this 8 years ago. Was searching for hours for a way to do this. Works.

avatar image SoBiT · Feb 07, 2013 at 12:48 PM 0
Share

Thanks, this worked.

avatar image Screenhog · Feb 07, 2013 at 05:20 PM 1
Share

For moving a fixed distance, there's also Vector3.$$anonymous$$oveTowards.

Show more comments
avatar image
0

Answer by Zarenityx · Feb 07, 2013 at 12:04 PM

When you are using Transform.LookAt, it will always point the positive Z axis. If you don't want to worry about collisions, just use Transform.Translate. Try this if you don't want it jittering when it gets really close, or if you want it stopping within a radius.

 var target : Transform;
 var speed : float;
 var radius : float;
 function Update(){
 transform.LookAt(target.position);
 if(Vector3.Distance(transform.position,target.position)>radius){
    transform.Translate(0,0,speed);
 }
 }

 
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
avatar image SoBiT · Feb 07, 2013 at 12:10 PM 0
Share

What does Translate exactly do? There is a speed? I don't need a smooth following. It can also teleport to the new position, this doesn't matter :)

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

13 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

Related Questions

Sigh... Need help with moving object a distance over time 1 Answer

Rotate object with another object 0 Answers

Getting vectors on an object every so many units 0 Answers

Translate object on y axis 0 Answers

how to keep an animated object from moving to 0.0.0 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