I’m using Unity2D for this, but I’m sure it’s the same in 3D
I’m trying to create a platformer game and when my enemies run into my player… they start pushing my player agressively.
How do I make it so the Player and the Enemy Collide, but don’t push each-other around?
Just, stop?
2 Answers
2
Do the player and the enemy both have rigidbodies? If so, you can 0 out the velocity vector of the rigid bodies. If this was on the player and the enemy was tagged “Enemy”:
void OnCollisionEnter( Collider other )
{
if( other.gameObject.tag == "Enemy" )
{
rigidbody2D.velocity = new Vector2( 0,0 );
other.rigidbody2D.velocity = new Vector( 0,0 );
}
}
So you have a couple of choices. Firstly you could give your player a very high mass meaning that the enemies can’t push it. However, it will send enemies flying should it move into them.
Secondly, it’s a platformer, I never use physics for platformers - I like to be really in control. So you make the rigidbody kinematic and don’t allow it to move through blockages using OnCollisionEnter or by raycasting. Its more work though.
Good point - converted to Answer - though probably only in one direction right? Otherwise there might be weird vertical "sticking" going on.
– whydoidoit