- Home /
Restrict two objects from getting too close.
I previously made this 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 within a certain distance of each other.
I originally had this 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, which I have done by just increasing the objects but, if they are currently next to each other when this 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 this 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!
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 ins$$anonymous$$d 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?
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)