Rotating FirePoint

I’ve had to change my Movement script from the ground up, and am having problems flipping the direction that his bullet fires in. His melee attack points and animations flip just fine, even where the bullet spawns flips, but it always flies right (into the player’s face)

Is there a way to fire the bullets in the correct direction? Here is my script:

   private void UpdateAnimationState()
    {
  if (dirX > 0f)
        {
anim.SetBool("running", true);
transform.localScale = new Vector2(1f, 1);
        }
        else if (dirX < 0f)
        {
            anim.SetBool("running", true);
            transform.localScale = new Vector2(-1f, 1);
        }
        else
        {
            anim.SetBool("running", false);
        }
    }

Hope you guys can help. I can figure things out but I’m new and this has just felt like an impossible thing that should be do-able

Of course there is.

Nothing in the code above affects the bullet’s velocity.

Steps to success:

  • identify where the bullet is given its initial velocity

  • reason about why it is correct when facing right

  • when you understand that, try and fire left and see what is NOT properly shifting to move to the left.

  • adjust the code so that it fires correctly when flipped

Obviously, motion in the X axis is what you care about here, assuming a “normal” camera looking down +Z with +Y being upwards.

This is my shooting code,

void Shoot ()
    {
        animator.SetTrigger("Shoot");
        if (rb.transform.localScale.x < 0)
{Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
rb.GetComponent<Rigidbody2D>().velocity = new Vector2(-20, 0) ;
         Debug.Log("You're looking Left");}

          else if (rb.transform.localScale.x > 0)
          {Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
rb.GetComponent<Rigidbody2D>().velocity = new Vector2(-20, 0) ;
         Debug.Log("You're looking Right");}
    }
    }

I can tell from the Debug Log that it is registering whether I am facing left or right. So I tried changing the numbers in the X axis n the velocity line, but they still hit my player’s face.

I’ve been working on this for 4 hours so I need to take a small rest but I will analyze it more in a moment

I’m not sure you can flip the scale of a Rigidbody like that to make it negative.

I’ve certainly never tried that.

Usually one only flips the VISIBLE part of things, as a child GameObject, not the entire structure including Rigidbody.

But regardless, if if that IS legal, you’re still setting the same X velocity both left and right!! Of course it will be the same.

8007320--1030121--same.png

2 Likes

I have several additional issues with your code. First of all what is “rb”? I guess this should be a rigidbody or rigidbody2d reference, no? Why do you call GetComponent<Rigidbody2D> on it? That seems weird. Even assuming the GetComponent call is redundant, what does rb actually refer to in the first place? You instantiate your bulletPrefab. The Instantiate call actually returns the newly created object. So I would assum you want to set the velocity of the bullet, however you can not possibly have a reference to the new bullet that way.

Does the bullet propel itself forwards?

1 Like

Yeah, I noticed that was weird and I found out that line didn’t actually do anything so I commented it out
It’s kind of funny how bad I am at coding, anyways

Yes, the bullet by default just goes right at a certain speed for a certain amount of time, like so

   void Start()
    {
        StartCoroutine(CountDownTimer());
        rb.velocity = transform.right * speed;
    }

Maybe I can solve it there by making it go left if facing left ??
I did manage to get it to shoot the direction of the player by adding a Quaternion.Euler that changes the rotation, which was just 2 easy lines of code, but for some reason the first bullet shot after switching directions still smacks my player in the face going the wrong direction

void Shoot ()
    {
        //firePointL.transform.eularAngles.y = 180;

        animator.SetTrigger("Shoot");
        if (rb.transform.localScale.x < 0)
{Instantiate(bulletPrefab, firePoint.position, firePointL.rotation);
firePointL.transform.localRotation = Quaternion.Euler(0,0,180);
//bulletPrefab.GetComponent<Rigidbody2D>().velocity = new Vector2(-20, 0) ;
         Debug.Log("You're looking Left");}

          else if (rb.transform.localScale.x > 0)
          {Instantiate(bulletPrefab, firePoint.position, firePointR.rotation);
          firePointR.transform.localRotation = Quaternion.Euler(0,0,0);
// bulletPrefab.GetComponent<Rigidbody2D>().velocity = new Vector2(20, 0) ;
         Debug.Log("You're looking Right");}
    }
    }

Maybe it’s easier to make the bullet spawn going in the proper direction than to fix this. I don’t really know

Yes that was me experimenting but ultimately that line of code was useless anyways.
Now I’m trying to rotate the firePoint using a Quaternion.Euler, but it’s giving me bugs
So I am considering having the bullet spawn going to the left if the player is facing the left, as by default it goes right (As shown in the reply above this comment). But I don’t know how

Edit: Alright I got it, I had to put the Quaternion.Euler before the Instantiate line. Here is the code that allowed me to rotate the firing point (For anyone who sees this in the future, it’s basically just two easy lines of code)

   void Shoot ()
    {
        animator.SetTrigger("Shoot");
        if (rb.transform.localScale.x < 0)
     
{firePointL.transform.localRotation = Quaternion.Euler(0,0,180);
    Instantiate(bulletPrefab, firePoint.position, firePointL.rotation);
         Debug.Log("You're looking Left");}

          else if (rb.transform.localScale.x > 0)
          {firePointR.transform.localRotation = Quaternion.Euler(0,0,0);
              Instantiate(bulletPrefab, firePoint.position, firePointR.rotation);
         Debug.Log("You're looking Right");}
    }
    }

This just makes not much sense again ^^. When you call Instantiate you create a new object and the position / rotation values you supply here are applied / copied to the new object. You now added the second line here where you change the local rotation of your firepoint after you instantiated your bullet. That makes just as much sense as pulling the trigger on a hand gun and after that you start pointing it in a certain direction. Apart from that I’m note sure how your “firePoint” object is setup. However if you want to apply an additional 180° rotation you should multiply the rotation by that quaternion and not set the rotation.

If you don’t want to provide the final rotation in the instantiate line you can change the rotation after you created the new bullet. However as I said above, the Instantiate method returns a reference to the new object which you currently completely ignore. So either do

if (rb.transform.localScale.x < 0)
{
    Instantiate(bulletPrefab, firePoint.position, Quaternion.Euler(0,0,180f) * firePointL.rotation);
}
else
{
    Instantiate(bulletPrefab, firePoint.position, firePointR.rotation);
}

or if you want to change the rotation afterwards you can do

if (rb.transform.localScale.x < 0)
{
    var newBullet = Instantiate(bulletPrefab, firePoint.position, firePointL.rotation);
    newBullet.transform.rotation *= Quaternion.Euler(0,0,180f);
}
else
{
    Instantiate(bulletPrefab, firePoint.position, firePointR.rotation);
}

ps: you really should work on your code formatting. Your opening and closing curly braces stuck at the front or the end of a line is just horrible to read.

Thanks to all for your informative replies.
I am going to bookmark this thread https://forum.unity.com/threads/rotating -bik-firepoint.
Thanks.

1 Like