Skip to main content

I want to shoot thea 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).

thisThis is my gunController class -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))); 
    }

 
}

thisThis is my Projectile Controller class -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

}

I want to shoot the 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 Projectile Controller 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

}

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

}
Source Link
AJ123
  • 189
  • 1
  • 3
  • 13

Projectile Direction Unity

I want to shoot the 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 Projectile Controller 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

}