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
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 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.
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
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
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)
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
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.