So I have a gun script the follows the mouse and I add force to the right, but in my character controller script if my player moves left it change the scale from 1 to -1 basically changing where the player is looking. So when I move left the gun also faces left but when I shoot the bullet goes right any help? Here is the code:
public class ShootGun : MonoBehaviour
{
public Transform Gun;
Vector2 direction;
public GameObject Bullet;
public float BulletSpeed;
public Transform ShootPoint;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = mousePos - (Vector2)Gun.position;
FaceMouse();
if (Input.GetMouseButtonDown(0))
{
shoot();
}
}
void FaceMouse()
{
Gun.transform.right = direction;
}
void shoot()
{
GameObject BulletIns = Instantiate(Bullet, ShootPoint.position, ShootPoint.rotation);
BulletIns.GetComponent().AddForce(BulletIns.transform.right * BulletSpeed);
}
}
↧