How To Add Knockback To Player Once Gun Fired In 2D?
Hey Guys, so for a couple of days i've been trying to add a knockback to the player once the gun has been fired, i have no idea how to do it, I've tried a couple of ways but it doesn't work
Here is the code for the gun:
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);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612232606im_/https://answers.unity.com/themes/thub/images/avi.jpg)