- Home /
How to reset child object's rotation without changing parent's
Hello Unity Community!
Okay, so I have kind of a noob question here: This is what I need to accomplish: In my platformer game, I have portal objects that lead you to the next level, but I don't want the player to get mowed by an enemy as soon as they enter the next level, so I added an empty game object with a collider to serve as the player's "Safe Zone", and I have a method to find out what location it should be at based on the level the portal takes you to. My Problem: I need the safe zone to be upright, but not all of my portals are upright, and I'm too unfamiliar with Unity's rotation system to figure out how to get it upright.
private void Start()
{
levelTracker = FindObjectOfType<LevelTracker>();
safeZone = transform.GetChild(0);
Debug.Log(transform.rotation.z);
safeZone.Rotate(____________________); // Problem spot is here.
Vector2 position = new Vector2(0, 0);
// Vector2 position = levelTracker.GetLevelLocation(levelNumber);
// position += new Vector2(0f, 0.6f);
safeZone.position = position;
}
Can somebody explain to me how to do this considering I don't yet know how Unity's rotation metrics work?
Answer by Llama_w_2Ls · May 03, 2021 at 10:04 AM
In the editor, just by manually rotating the object, what is the rotation you need to apply to make it upright? If it is (0, 0, 0), you can use Quaternion.Euler
to create a rotation equal to zero rotation. For example:
// Object is rotated 0 on x, 0 on y, and 0 on z - No rotation
safeZone.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
If the angle you need to apply is (0, 90, 0), for example, just change the code to:
// Object is rotated 0 on x, 90 on y, and 0 on z - No rotation
safeZone.rotation = Quaternion.Euler(new Vector3(0, 90, 0));
Yep! This worked, and set the rotation in world space and not relative space just like I wanted it to. Thanks!
Your answer
Follow this Question
Related Questions
Delayed rotation when i use transform.rotate 1 Answer
Strange rotation pattern. 0 Answers
How can I rotate an object without moving it up or down? 0 Answers
managing rotation of Turret on two axis 1 Answer
When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers