Collision without "interaction" (Pushing)

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 );
}
}

Good point - converted to Answer - though probably only in one direction right? Otherwise there might be weird vertical "sticking" going on.

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.

This over-all, just sounds like a horrible idea. I haven't been developing for long, however something about what you just said doesn't sound right. Could you explain the advantages to doing this? What about moving up and down slopes, etc?

If you haven't been developing long try using CharacterController which does what I suggest - though I can't say it's my favourite way of doing stuff, at least someone else has done the work on the raycasting, etc. I don't know what you mean about slopes - normally you find the floor by raycasting down and move the player to rest on that - you stop movement by raycasting from a certain height above the floor, a certain distance out - this is set up to miss natural rises in the ground.

The point is that physics and platformers rarely go together because you actual don't want real physics as it isn't that fun and is not what people expect when they play