Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ROM · Feb 01, 2011 at 10:26 PM · scaledistancepointtrigonometry

Fixed distance between objects?

Is it possible to set it so two objects that are both affected by physics are a fixed distance between each other programmatically (On a 2D plane)? I basically want it so they are always 10 units away from each other. I'm sure that I could do it with joints but i'd rather not and if possible do it like:

if(Vector2.Distance(PointA, PointB)<10 || Vector2.Distance(PointA, PointB)>10){
// DoSomething
}

But I'm not sure what I could do inside the if statement and how I could scale the x and y values so the hypotenuse/distance is always 10. Does anyone have any ideas?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DaveA · Feb 01, 2011 at 10:53 PM

What if you parented them to the same object, offset by 10 units, then had physics affect the parent?

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
1

Answer by Bunny83 · Feb 01, 2011 at 11:24 PM

Why don't you want to use joints? That's what they are made for. If you want to move them manually, you have to care about the right physics yourself. That means you have to add forces to both objects that push them apart or pull them together. But it's not that easy. Forces always have a dynamic behaviour. if you want them always be at a fix distance you have to apply a huge force to compensate the "offset". But that can cause your objects to jitter and if you apply only small forces the "correction" is not instant.

Anyway, you have to decide what you want to do. That's the basic idea:

I guess JS? And i guess you want to move on the x-z-plane? y points upwards.

var secondObject : GameObject; var desiredDistance : float = 10; var force : float = 5;

function FixedUpdate() { var pos1 : Vector3 = transform.position; var pos2 : Vector3 = secondObject.transform.position; var V : Vector3 = pos2 - pos1; float dist = V.magnitude;

float offset = desiredDistance - dist;

rigidbody.AddForce(-V.normalized*offset*force); secondObject.rigidbody.AddForce(V.normalized*offset*force); }

That would apply the same force to both objects, but again: why do you want to do that inefficient and not really physically correct yourself? Unity comes with Nvidia's PhysX engine so why not use a joint?

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 ROM · Feb 01, 2011 at 11:32 PM 0
Share

Thanks for the help! Both of the objects are currently joined by a configurable joint which I am finding challenging to set a hard and fast distance between the two objects. If it's not too much bother do you know of a way of doing it with this kind of joint? Thanks again!

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

No one has followed this question yet.

Related Questions

Set plane scaling pivot point? 1 Answer

spawned object wont change scale? 1 Answer

Find center between 4 points 3 Answers

Get new Point from Vector and Angle 1 Answer

finding point on the vector 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