Home > Back-end >  How do I knock back my player when they shoot?
How do I knock back my player when they shoot?

Time:09-30

I want to add knockback to my entire player when it shoots, so it can impulse itself.

I'm new to making games, I don't know much about c# and I haven't found any tutorials for the thing I need

CodePudding user response:

Difficult to know what you need help on here. But try to I'll answer the question.

When your player fires their gun, you would typically create a new object and apply a force to it for slow move projectiles or alternatively, you'll do a ray trace to see if the target is hit for laser beam style shots.

To make the player move, use AddForce on their Rigid Body to apply a force in the opposite direction. That direction is simply the negative direction of the projectile's direction (or the ray that was traced)

CodePudding user response:

If its a 2d game, this will work:

Select a the bullet. At the about top, there is a tag button. Click the untagged button (if it is) and then add a new tag and then write "Bullet" in one of the boxes. Again select the bullet and again click on untagged and then a button named "Bullet" appears. Click on it. Now write this in the enemy script.

if (other.gameObject.CompareTag("Bullet"))

{
    
    transform.position  = Vector3.left * 2.5f;
    transform.position  = Vector3.up * 0.15f;

}

Make sure that your player has a rigidbody2d attached.

You can change the values after the multiplication signs (2.5f and 0.15f) to make the knockback longer/shorter.

  • Related