- Home /
Stop Rigidbody From Being Pushed By Another But Still Move With Physics
Hi,
What is the "Unity correct" way to move an object with physics but make it unmovable to identical objects in the scene?
For instance, if a character is moved by physics and can move around freely, I want it to treat other characters as immovable objects even though they have similar masses.
Thanks,
Answer by LW · Oct 27, 2018 at 05:10 PM
So here is the solution I ended up using if it can help anyone else:
D = collider.position - position;
velocity -= normalize(D) * Dot(velocity, D)
It's not perfect but it gets the job mostly done. I hope that helps.
Answer by MonoFlauta · Oct 17, 2018 at 03:33 PM
You can set the Layer-based collision: https://docs.unity3d.com/Manual/LayerBasedCollision.html
Basically, you set a layer for each object and in Edit>ProjectSettings>Physics you set which layer can collide with which one. If you want that two same objects dont collide. Just make sure same layer isn't active in the Layer Collision Matrix
This is an answer to a different question, unfortunately.
$$anonymous$$y question is specifically: how do I treat some objects as immovable and still collide with time?.
- Yes, they all can move.
- No, I cannot change their mass.
You want an object to collide with another one (not time I guess?) but that cannot be moved by others object like if the mass was "too big" when colliding with others. If now I understood well, you should save the velocity of the rigidbody in each frame. If it collides with something that shouldn't move it, you set the speed as before the velocity as before the collision. That should be just saving the Vector3 value of the rigidbody.velocity and when colliding with something you don't want to move you, put the rigidbody.velocity = to that value you saved. You should save that value in the fixed update because is part of the physics. If that isn't yet the answer you where looking for, please provide a step to step of things you want to happen so I can try to understand better.
Sorry for my poor explanation @$$anonymous$$onoFlauta.
I have two characters.
They both move with physics.
When either one runs into the other it to act as if the other is immovable.