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
↧