- Home /
Instantiate rotation is not correct, why?
I have a Player that shoots bullets from a gun. Depending on the gun, there are different amounts of bullets. With my Shoot() method I want the instantiated bullets to have a fixed spread each time they shoot.
I tried that with a for loop:
void shoot()
{
//Set Spread
float spread = 10;
float currentSpread = 0;
//Instantiate Bullet
for (int i = 0; i < amount; i++)
{
//Get Spread
if (i == 0)
{
currentSpread = 0;
}
else if (i == 1)
{
currentSpread = spread;
}
else if (i == 2)
{
currentSpread = -spread;
}
else if (i == 3)
{
currentSpread = spread * 2;
}
else if (i == 4)
{
currentSpread = -spread * 2;
};
//Instantiate
Bullet = Instantiate(PreFab, Canvas.position, Quaternion.Euler(0, 0, Player.rotation.y + currentSpread));
//Set Parent
Bullet.transform.SetParent(Canvas, false);
//Set Name
Bullet.gameObject.name = "TeamBulletOne";
}
}
The "Canvas" is where the Bullets (2d) spawn, the rest of the world is 3d.
However, when I shoot it randomly either instantiates the bullets with a slightly off rotation or they spawn with about 2 degrees difference, not with the 10 degrees that are set in the Shoot() position.
I have no idea why and no matter how often I change or go through the method, I can't find out why it isn't working...
I don´t have anything else that affects the shooting mechanism, the colliders on my bullets are set to ignore the player and the other bullets and gravity is of as well.
Does anyone have an idea what to do?
Answer by metalted · Dec 06, 2021 at 09:35 PM
You might want to try Quaternion.AngleAxis(player.up, currentSpread);
. This will create a rotation around the player y-axis, with a certain degree. You might get weird results because player.rotation.y
is the y component of a Quaternion, and Quaternions are 4 dimensional hocus pocus. Its not the same as player.transform.localEulerAngles, which is the rotation you see in the inspector, which you can't really use because of the rollback to 0 after 360 (or -180, 180 not sure atm).
Also, I wanted to say, instead of using -else if-, you could use -switch- statement. But what would be even better is using the following ternary operator: currentSpread = i % 2 == 0 ? (i / 2) * -spread : (i + 1) / 2 * spread;
It is the same as your if else, but you dont need to rewrite for every i value. (If you are not familiar with the ternary operator: value = condition ? if condition is true : if condition is false
. So in this case: currentSpread = i is even ? calculate value for even i : calculate value for odd i
. )
The ternary operator is a great improvement I didn't think of, thank you! The Quaternion.AngleAxis(currentSpread, player.up)
though works for the z-axis but generates random rotations for the other axis as well. I tried setting x and y rotation zu 0, but then I just get the same results as before...
I move my bullet with Rigidbody.AddForce(transform.forward)
, could it be that this causes the problem?
EDIT: Tried with CharacterController and transform.Translate, both didn't solve my problem, instead made the bullet behavior even weirder
Oh my bad, Quaternion.AngleAxis is not really the right way to go. I wasn't able to test it. Maybe this is what you are looking for: transform.rotation * Quaternion.Euler(0, currentSpread, 0)
. This will basically take the players rotation, and rotate around the players y axis 'currentSpread' degrees.
I see you wanted to rotate around the z-axis. Doesnt really make a difference. I guess the code you want to use is as follows: Bullet = Instantiate(PreFab, Canvas.position, Player.rotation * Quaternion.Euler(0, 0, currentSpread));
I tried that and additionally had to adjust some rotations //Instantiate Bullet = Instantiate(PreFab, Canvas.position, Player.rotation * Quaternion.Euler(0, 0, currentSpread));
Fix Rotation Bullet.transform.rotation = Quaternion.Euler(0, 0, currentSpread); Bullet.transform.localRotation = Quaternion.Euler(Bullet.transform.localRotation.x, Bullet.transform.localRotation.y, currentSpread);
Now it works better than before, so thanks! It still gives a little bit off z rotations (9,894 instead of 10 for example, no idea why), but that's fine for now!
Your answer
Follow this Question
Related Questions
Shooting with variable vector3 0 Answers
Shoot in proper direction with spread? 1 Answer
Shooting bullets in different patterns 0 Answers
Clone positioning inside loop 1 Answer