- Home /
How can i hide weapon behind character sprite while he is looking up?(Top-down shooter)
I'm brand new to unity and trying to learn it making little project that contains everything i have learnt. I really enjoy top-down shooters like Enter the Gungeon and i decided to take it as basis for my test project. I have already made character controller and weapon facing the mouse position and now i want weapon sprite to change an order in sorting layer when my character is looking up to hide the weapon behind character sprite.
I have this very handsome orange square and was trying to do the same thing with my current knowledge using SpriteRenderer.sortingOrder in if statement with .transform.rotation.z but it doesn't work for some reason.
if (gunTransform.rotation.z < 150f && gunTransform.rotation.z > 32f)
{
gunSpriteRenderer.sortingOrder = 0;
}
else
{
gunSpriteRenderer.sortingOrder = 2;
}
Please show me what goes wrong here or suggest another solution of my problem. I will be very grateful.)
Answer by Vivien_Lynn · May 15, 2021 at 11:28 AM
That is because gunTransform.rotation.z
returns values between -1 and +1. What you want to use is gunTransform.eulerAngles.z
, which returns values from 0 to 360. To see this in action, you can use a Debug-Message, like so:
float angle = gunTransform.eulerAngles.z;
Debug.Log(angle);
if (angle < 150f && angle > 32f)
{
gunSpriteRenderer.sortingOrder = 0;
}
else
{
gunSpriteRenderer.sortingOrder = 2;
}
You can read more about rotation here: Transform.rotation.
Off-topic but maybe relevant / useful to you is that there it says: "To rotate a Transform, use Transform.Rotate, which uses Euler Angles.".
I had better check how does code works before using it by myself. Thank you, buddy, you helped a lot.
Your answer

Follow this Question
Related Questions
Place UI text on top of other elements in 2D 1 Answer
Rendering order approach for a 2.5D game. 2 Answers
2d Animation not rendering in front of background sprite in spite of sorting layers 1 Answer
Problem with 2D gun rotation script (Would really apreaciate help) 1 Answer
Nested sorting layers? 1 Answer