0
\$\begingroup\$

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

}
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$
Instantiate(bullet, guntip.position, guntip.rotation);

You need to inherit rotation of guntip to make bullets face that way.


To make bullet go towards the direction it's facing, if your 2D setup is top-down:

 MyRB.AddForce(transform.up * rocketSpeed, ForceMode2D.Impulse);

If your 2D setup is side-scroller:

 MyRB.AddForce(transform.right * rocketSpeed, ForceMode2D.Impulse);
\$\endgroup\$
9
  • \$\begingroup\$ Thanks it is facing the right direction, but it is not moving, like before. \$\endgroup\$ Commented Jul 6, 2016 at 5:35
  • \$\begingroup\$ @AJ123 Try increasing rocketSpeed value. \$\endgroup\$ Commented Jul 6, 2016 at 8:42
  • \$\begingroup\$ It faces the right way but it always goes vertical. \$\endgroup\$ Commented Jul 6, 2016 at 9:50
  • \$\begingroup\$ Do you understand what this does? MyRB.AddForce(new Vector2(0, 1) * rocketSpeed, ForceMode2D.Impulse); Its taken from your code, so you should. Why would your projectile go in any other direction than vertical? That is how you wrote it. \$\endgroup\$ Commented Jul 6, 2016 at 12:08
  • \$\begingroup\$ I know, but how do I make it go the direction it's facing \$\endgroup\$ Commented Jul 6, 2016 at 13:44
0
\$\begingroup\$

Your Awake() method at ProjectileController class should be like:

void Awake()
{
    MyRB = GetComponent<Rigidbody2D>();

    Vector2 direction = GameObject.Find("Gun").GetComponenet<GunController>().guntip.position - transform.position; //Get a direction vector to fire the bullet at.

    direction.Normalized(); // direction vector normalized to magnitude 1

    MyRB.AddForce(direction * rocketSpeed, ForceMode2D.Impulse);
}

And Instantiation in GunController() should be like:

void fireRocket()
{
    if(Time.time > nextFire)
    {
        nextFire = Time.time + fireRate;

        Instantiate(bullet, transform.position, Quaternion.identity); 
    }
}

You need a direction from GunController class's guntip. Name the gun object as "Gun" (or change in code) and need to get the gun tip position so that you can get a direction to shoot the projectile at. When we are subtracting projectile's position vector from guntip's position vector, we get a direction from projectile's current position to guntip's position which is the direction to shoot projectile. But it has some magnitude in it. So we have to make the magnitude to 1 so that it doesn't effect the force you are about to apply on it. That's why Normalized() is used on the direction so the direction vector is now unit vector with just direction in it(magnitude 1). Then AddForce() on that direction.

\$\endgroup\$
2
  • \$\begingroup\$ that does not work. \$\endgroup\$ Commented Jul 6, 2016 at 10:46
  • \$\begingroup\$ What the result? Have you put guntip as Gun object's child? \$\endgroup\$ Commented Jul 6, 2016 at 15:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.