I want to shoot a projectile in the direction the gun is facing.
With my current script it only shoots vertically. (I can also make it go horizontally but that's it).
This is my GunController class:
public Transform guntip;
public GameObject bullet;
float fireRate = 0.5f;
float nextFire = 0f;
void Update ()
{
//playershooting
if (Input.GetAxisRaw("Fire1") > 0)
{
fireRocket();
}
}
void fireRocket()
{
if(Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(bullet, guntip.position, Quaternion.Euler(new Vector3(0, 0, 0)));
}
}
This is my ProjectileController class:
Rigidbody2D MyRB;
public float rocketSpeed;
void Awake()
{
MyRB = GetComponent<Rigidbody2D>();
MyRB.AddForce(new Vector2(0, 1) * rocketSpeed, ForceMode2D.Impulse); // explosive force, change vector 2 for horizontal shooting
}