- Home /
Object keeps moving after hit
I'm creating a asteroid and when the player collides with the asteroid, I want the asteroid to explode and push the player away to make it look like they are bouncing off the asteroid. This is what I've got:
void OnTriggerEnter2D(Collider2D target)
{
if(target.tag == "Player")
{
target.GetComponent<PlayerHealth>().DecreaseHealth(4);
DestroyMediumAsteroid();
BouncePlayer(target);
}
}
void BouncePlayer(Collider2D target)
{
Rigidbody2D playerRig = target.GetComponent<Rigidbody2D>();
Vector3 direction = (transform.position - target.transform.position).normalized;
playerRig.AddForce(-direction * 10, ForceMode2D.Impulse);
}
It works at pushing the player away but the player seems to keep moving in the same direction, even if I'm not touching the controls but it will be slowly. Of course I don't want this but I'm not sure how to fix it.
Any help?
Have you added a linear drag value (other than 0) to the rigidbody?
That's the problem. The drag of an object is how it loses velocity over time.
Set the linear drag to anything but 0 and it should have a slight fade off. Either that or save the direction the velocity goes in and apply force in the opposite direction afterwards.