Local euler angles not working properly

Local euler angles is returning wierd numbers such as 64,25,360,0 when it’s not even close. Is there a fix for this?

Debug.Log(transform.localEulerAngles);

What is wrong with the values?

I doubt that there is anything bugged about localEulerAngles, or a lot of projects would be very broken.

Could you give us examples of actual results from it? The one you’ve written there has 4 components, so it can’t have come from a Vector3.

Sure thing. What I’m trying to do is take the Y rotation and subtract 50f from it.
6879182--803480--upload_2021-2-25_23-50-59.png
6879182--803483--upload_2021-2-25_23-51-27.png

have you try to debug
transform.eulerAngles?

Those numbers don’t look too far off what’s in the Inspector.

One of the pains in the backside about using euler angles in 3D stuff is that there are multiple ways to represent the same rotations. In this case, the Y rotations being around 265 and -95 look wildly different, but they’re actually the same rotation because 360 + -95 = 265.

This makes implementing certain logic truly painful. For example, I can’t check if I’m looking to the right by seeing if localEulerAngles.y > 0 because plenty of positive numbers actually have it turned to the left.

With that in mind I strongly recommend learning how to use quaternions for rotation logic. Thankfully you don’t need to understand the math, you can just learn a few rules about what different operations do and go from there. To debug stuff you can always convert them back to eulers so they’re intuitive to read, just don’t operate on them in that format when you can avoid it.

What specifically are you trying to achieve? Someone may be able to point you in the right direction.

I’m attempting to make a coroutine that creates a burst of projectiles one going from the left,middle,and right.

So the trick is that it the projectile rotation starts on the left the works its way to the right.

Here’s what the coroutine looks like.

private IEnumerator Attack4()
    {  
        while(true)
        {
                Vector3 targetRotation = transform.rotation.eulerAngles;
                targetRotation.y = transform.rotation.y - 50f;;
                for (int i = 0; i < 3; i++)
                {
                Instantiate(projectile2,projectileOrigin.position,Quaternion.LookRotation(targetRotation));
                targetRotation.y = + 50f;
                yield return new WaitForSeconds();
               
            }
        }
    }

This produces the same result unfortunately.

Ok, I think your issue there is actually that you’re passing euler angles into Quaterion.LookRotation(…), which expects a direction vector. Both are Vector3s, but the numbers are interpreted very differently.

I think that if you change out Quaternion.LookRotation(…) for Quaternion.Euler(…) things might work better? Quaternion.Euler converts from euler angles to a Quaternion, which I think is what you’re actually looking for there.

1 Like

That did it! Thank you!

Here’s the working script, a messy version but it does a shotgun like spread now.

private IEnumerator Attack4()
    {  
        while(true)
        {
            if(!isAttack)
            {
                isAttack = true;
                Vector3 targetRotation = transform.rotation.eulerAngles;
                targetRotation.y = transform.rotation.eulerAngles.y - 10f;;
                Instantiate(projectile2,projectileOrigin.position,Quaternion.Euler(targetRotation));
                targetRotation.y += 10f;
                Instantiate(projectile2,projectileOrigin.position,Quaternion.Euler(targetRotation));
                targetRotation.y += 10f;
                Instantiate(projectile2,projectileOrigin.position,Quaternion.Euler(targetRotation));
                //yield return new WaitForSeconds(.1f);
                yield return new WaitForSeconds(2f);
                //}
                //yield return new WaitForSeconds(.5f);
                isAttack = false;
                //yield return new WaitForSeconds(enemyCoolDown3);
               
            }
        }
    }
1 Like

Thumbs up for coming back and sharing the solution!

There’s a lot of subtlety in the math for 3D scenes. Easy to get tripped up by stuff like that early on. Glad I could help!

1 Like