Hello,
so this is a question I came up with few days ago. It is not specific to any of the lectures but this could easily be part of one of the later 3D games that were developed during the Unity 3D course. I also asked this in the official Unity help room but did not get any answer so far. (https://answers.unity.com/questions/1571683/how-to-make-objects-block-each-other-without-being.html).
Its mostly about unitys physics engine, colliders and rigidbodies.
Lets pretend I want to develop a game that has these different types of gameobjects.
- Player Characters (multiple)
- Enemies
- Static Objects (World)
- And some gameplay objects that the player can pick up and move around
The physics interaction of these objects should be as follows:
- Some kind of gravity for Players, gameplay objects and enemies
- Enemies, Players and Gameplay objects should block each other
- This means that if a player or enemy (called gameobject A) moves against a player/enemy/gameplay object (gameobject B), those objects should collide with eath other but B should not move as a result of the collision with A. So a player/enemy should not be able to push other players, enemies or gameplay objects around. (These kind of interactions should be like collisions between rigidbodies and static colliders, as if they would move against a wall).
Now to the problem or (for me) confusing part:
As we know in Unity its recommended to add a rigidbody (RB) component to any gameobject that has a collider and moves. This is due to performance reasons and to have these kind of objects not being treated as static colliders. In this example this means that players, enemies and gameplay objects all need to have a rigidbody component. With an RB attached they can now detect collisions with other colliders and do not simply ignore collisions and fall through the ground, etc. So thats good. But the problem now is that all of these objects can be pushed by each other through physics. For example when a player moves against another player or gameplay object as a result that object is moved by the moving player because of the collision and the resulting force being applied to these gameobjects. This is what i do not want. I just want them to collide (meaning that they cannot move through each other) but they should not be pushed by other gameobjects.
There are some “solutions” that i was experimenting with but did not achieve what i wanted. Like making (some of) them kinematic, which makes them not being pushed by rigidbodies (which is good) but they will no longer interact with each other because kinemaitc -> kinematic wont collide, so they just move through each other. Gravity also do not affect those objects anymore.
Another approach was using the constraints and locking the RB rotation and tranlation. This works half way. The good part is that the objects block each other and are also not pushed by others. But in Unity it is suggested to move an rigidbody object using his rb. By either using rb.moveposition or rb.addforce / rb.velocity = someVec3. But if the RB translation is locked the gameobjects wont be able to move. You would need to move them via the Transform component which is considered a no go for RB objects.This can also lead to wrong colliision detection as far as i undestood.
(Sidenote: And what if i simply want to move them using addforce because the movement should behave like that?)
So this cant be the right solution either.
I hope you understand what my question is about. I really dont understand what the default approach should look like for this kind of problem in unity. I also do not think that this is a very specific problem because this can affect a lot of games. So there must be solutions to this in unity. There are plenty of games that have different moving objects (in unity these should be RBs). And if those objects collide with eath other they do not move through each other and they also do not push each other around. They just behave like they would move against a “wall”.