• 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 emorainbow26 · Dec 22, 2015 at 09:09 AM · c#collisionphysicsrigidbodydistance

Restrict two objects from getting too close.

I previously made t$$anonymous$$s question but it doesn't appear to have gone through, if it's in the wrong place please move it.

I want to have two movable objects (Controlled by different analog sticks) that cannot get too close to eachother. Basically, they can go as far apart as they want, just not wit$$anonymous$$n a certain distance of each other.

I originally had t$$anonymous$$s done by having them both use a rigidbody and collider, so that they would just not pass through eachother, but I had some issues with the collisions firing because neither of them were kinematic and they just passed through eachother.

Currently it's set so that if they collide, they move to different sides, but that's not very effective and causes some unwanted occurrences.

Additionally I want the restricted zone to increase if the player messes up, w$$anonymous$$ch I have done by just increasing the objects but, if they are currently next to each other when t$$anonymous$$s happens, they wont move apart.

How can I make it so that the objects cannot get too close to eachother, but still be able to move around and have t$$anonymous$$s restricted zone increase at certain moments? Is there a certain script or physics object I can use to make sure they don't get too close? I tried using various ones like the spring and distance joint, but those just do the opposite of what I want.

Any help is much appreciated, thank you!

Comment

People who like this

0 Show 1
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 emorainbow26 · Dec 21, 2015 at 10:24 PM 0
Share

To clarify better, the reason the objects would pass through each other was because I had it so that they couldn't move each other (Like if I moved one into the other, it wouldn't push it aside), which was the original issue, using the trigger instead of normal rigidbody. So additionally, is there a way I can make it so that the objects also won't push each other out of they way?

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by G4merSylver · Dec 23, 2015 at 01:29 PM

I would recommend using rigidbody.addforce(target).

Vector3 target = object1.transform.position - object2.transform.position target = new vector3(-target.x, -target.y, -target.z)

Comment

People who like this

0 Show 0 · 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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Handling colliders on hundreds of asteroids (not procedural asteroids) ...URGENT 3 Answers

Issue with adding force and lerping position? Possible unity bug? 0 Answers

What is the best way to prevent a physics object from going through thin colliders? 2 Answers

Raycast doesnt detect object in front of rigidbody (player is stuck on wall) 1 Answer

How to change CC script to Rigidbody script 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