- Home /
How to make two objects not go through each other (without physics)
I have two gameobjects, both with colliders on them and one with a rigidbody attached.
I am controlling one of them by changing its position. When I move one gameobject into the rigidbody gameobject, it passes through. How can I make it so that the two objects touch but never go through each other without the use of physics .
I assume it might work like this:
Detect collision
Record position and orientation at collision
if next movement will move object into the other, reset position to recorded position
Any ideas if this is the right way and how to implement this? Thanks
Answer by Bunny83 · Aug 24, 2018 at 11:32 PM
Your question is very confusing. A rigidbody is a physics object. Any sort of collision is physics. So i'm not sure what you mean by "without physics". In order to properly detect collisions any moving object with a collider need to have a rigidbody attached.
Maybe you want no collision response? This is generally a bit difficult with physics. Maybe you want to use a CharacterController? The CharacterController is specifically designed to only detect collisions without being affected by other objects. Though a CharacterController has some restrictions. It always is a capsule and that capsule is always upright.
The behaviour you want is not really clear from your question.
The behaviour I want is such that when the two objects come into contact, they do not pass each other. What I mean by "without physics" is that I don't want to attach a rigidbody to the object that I am controlling because I am controlling it via transform.position. Is there a way to use a rigidbody AND control my object in this way (i.e. without applying force/torque)?
Answer by JVene · Aug 25, 2018 at 12:37 AM
While I agree generally with Bunny83, I think I can guess at your meaning.
Our confusion comes from the fact we've studied this type software extensively (I say type because I'm recent to Unity, but I've worked on various game and physics engines), and we know that collision itself is the first part of the physics engine, so it is physics. I believe what you're saying is that you're not moving objects by physics. The standard step which follows a collision is to calculate the forces exerted as a result of the collision and apply those to the objects such that they correctly respond to having collided, with reference to the nature of the materials involved.
From what I can see, you have the basic idea, but there are lots of tributaries in thought and design which are beyond the scope of a post. That can become the domain of writing physics engines, something that is barely within undergraduate studies of mathematics, physics and programming.
When objects collide there's a counterforce that results, and if the direction of that force isn't correct the results look odd if the intent is to present a realistic physical behavior. In games, however, this is occasionally unimportant. Your list of steps would exhibit this error when compared to real physical collisions, so you have to decide if that matters.
The subtle error you appear to have in your list, however, is the recorded position at collision. This depends on whether or not your collision objects is larger than your visible object, and the speed is such that the results happen to look right. Instead of recording the position at collision, one must consider either to use the previous position, before the collision, or to calculate that position which results in contact minus a very minor margin, so it looks as though the objects touch but don't overlap.
If the objects are simple enough, you may do well enough with a capsule or sphere collider. If that can do, you can avoid physics collisions altogether and work merely on the distance between the center of the objects in question relative to the radii of the spheres and/or capsules. It is much faster, but can be an oversimplification.
Answer by Stratosome · Aug 25, 2018 at 02:47 AM
The others have already made answers, but in case you're looking for something different...
I'd suggest leaving your Rigidbody component on the object and use Unity's physics (unless you are really wanting to avoid it or something). If your only problem is that since you are setting the position of it, and because you are, they are going through each other, try setting its position using Rigidbody.MovePosition. Assuming it is the rigidbody you are moving around, it will still collide and stop moving when it comes into contact with another collider.