- Home /
How Do I Add Knockback To Player Once Firing Gun In 2D?
hey so, for a couple of days i've been attempting to make a knockback to my player once the gun is fired. I can't get it to work,
here is the code for the gun:
using System.Collections; using System.Collections.Generic; using UnityEditorInternal; using UnityEngine; using UnityEngine.PlayerLoop;
public class GunMechanic : MonoBehaviour { public GameObject player; public GameObject bullet; public Transform pos; public Rigidbody2D rb;
 private void FixedUpdate()
 {
     //Rotate Gun
     Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
     difference.Normalize();
     float rotz = Mathf.Atan2(difference.y,difference.x) * Mathf.Rad2Deg;
     rotz += 180;
     transform.rotation = Quaternion.Euler(0f, 0f, rotz);
     if (rotz < -90 || rotz > 90)
     {
         if (player.transform.eulerAngles.y == 0)
         {
             transform.localRotation = Quaternion.Euler(180, 0, -rotz);
         }
         else if (player.transform.eulerAngles.y == 180)
         {
             transform.localRotation = Quaternion.Euler(180, 180, -rotz);
         }
     }
 }
 private void Update()
 {
  
     //shooting
     if (Input.GetMouseButtonDown(0))
     {
            
         Instantiate(bullet, pos.position, transform.rotation);
     }
 }
}
thanks in advance
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
Recoil for a third person shooter and active reticle 1 Answer
Gun Recoil 1 Answer
Gun Recoil C# 1 Answer
How do I modify my script to give my gun recoil when I shoot? 1 Answer
Play Animation on keypress? 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                