- Home /
2D: Child Object does not rotate like parent does
Okay, this is a weird question maybe and it is a really simple problem which could probably be solved in other ways really easily, but I googled a lot of questions now and tried many things and I don't understand what is going on, so the reason I am asking this question is to understand what is happening.
The perspective and the way the game is supposed to control is like the game "Nuclear Throne" for example. I have a parent "Player", a child "Pistol". This is all 2D, so these are sprites. I have a script which sets the rotation of the parent (Player) to 180 degrees on the Y axis when the mouse is on the left half of the screen and to 0 degrees when the mouse is on the right half of the screen with the following code:
void Update ()
{
if(Input.mousePosition.x < camera.pixelWidth/2)
{
transform.rotation = Quaternion.Euler(0, 180, 0);
}
else
{
transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
This does basically flip the sprite of the player horizontally, so the player sprite looks either left or right.
My Pistol has a script which makes it rotate always towards the mouse with the following code:
void Update()
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
This works. Since the pistol is a parent of the player, it also gets the 180 degree on the Y axis when I move the mouse to the left half of the screen. But even though it shows that the value for Y is 180, the sprite still looks like it isnt 180. When I am in Editor mode and just manually type in the value 180 for Y, I can see what the result would look like.
I think this might be because the one rotation might be overriding the other somehow? But why does it say 180 for Y rotation then? I first thought it is because "transform.rotation = Quaternion.Euler(0, 0, 0)" sets all rotation values of the child (pistol) to 0 0 0, but the other rotation (which happens on the Z axis btw) does still work.
Thanks in advance.
I think you may need to update localRotation ins$$anonymous$$d of rotation.
I didnt even knew localRotation existed. I checked out what it is in the Unity documentation, might work, will try. Thanks.
I changed the aim script of the pistol to change the localRotation ins$$anonymous$$d of the normal rotation and now it works ... kinda. It flips the pistol like it flips the player on the Y axis, and it still rotates around the Z axis, but the pistol aims at the opposite direction of the mouse, which makes sense I guess because we flip the pistol by 180 on Y. I currently try to figure out how to solve that.
Have you tried to scale the parent (like scale.x *= -1) to flip it on the x-axis?
that's a very bad idea. Never scale anything negative in Unity, you just get in trouble
Ok. I never did 2D games ... I thought negative scaling of sprites in 2D is comparable to flipping textures in 3D.
Answer by crowbar1 · Jan 17, 2016 at 05:30 PM
EDIT: I made a video to show what this all is about. What you see in the video is the RIGHT way: https://www.youtube.com/watch?v=Mk1rsgZuKyQ&feature=youtu.be
okay, after more research I finally did it...
I changed the code for my pistol like this:
void rotateToMousePosition()
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 newMousePos;
if (Input.mousePosition.x < camera.pixelWidth / 2)
{
float x = Input.mousePosition.x + (pos.x - Input.mousePosition.x) * 2;
float y = Input.mousePosition.y + (pos.y - Input.mousePosition.y) * 2;
newMousePos = new Vector3(x, y, 0);
Vector3 dir = newMousePos - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.localRotation = Quaternion.AngleAxis(-angle, Vector3.forward);
}
else
{
Vector3 dir = Input.mousePosition - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
The code for the player stays the same.
What I had to do is the following:
-change the localRotation of the pistol instead of the normal rotation (thanks Dave29483!)
-when the mouse is on the left half of the screen, aim at the opposite coordinates of the mouse
float x = Input.mousePosition.x + (pos.x - Input.mousePosition.x) * 2;
float y = Input.mousePosition.y + (pos.y - Input.mousePosition.y) * 2;
newMousePos = new Vector3(x, y, 0);
-when the mouse is on the left half of the screen, rotate by -angle instead of angle
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; // Winkel um den rotiert werden muss
transform.localRotation = Quaternion.AngleAxis(-angle, Vector3.forward); // Rotation
seems like a ton of work for such a simple problem... but I guess it's fine.
Answer by hexagonius · Jan 15, 2016 at 12:26 PM
I think the problem is order of execution. You are running two scripts on two gameobjects, both with an Update function. Unity does not care about any order of those. I'm pretty sure that your pistol updates prior to the player. Since it's childed to it, it will be rotated a second time along with the player.
You have two options:
you go into the ProjectSettings -> ScriptExecutionOrder and "tell" Unity in which order you want your scripts to be executed
(Better approach) Have just one script first rotating the player, THEN rotating the pistol
What I don't get though is why the pistol even rotates. I thought it's just attached to the player which looks towards the mouse automatically moving the pistol with him.
the player doesnt look around, its not really top down, its like a fake isometric game, but the pistol actually looks towards the mouse like a top down game like Hotline $$anonymous$$iami. Its hard to explain for me, if you want look up some gameplay of the game "Nuclear Throne".
Anyway, thanks for the answer, that might be it actually, will try this out.
Okay I don't think this is the problem. I changed the aim script of the pistol to change the localRotation ins$$anonymous$$d of the normal rotation and now it works ... kinda. It flips the pistol like it flips the player on the Y axis, and it still rotates around the Z axis, but the pistol aims at the opposite direction of the mouse, which makes sense I guess because we flip the pistol by 180 on Y. I currently try to figure out how to solve that.
$$anonymous$$ake the Player visual also child and flip it ins$$anonymous$$d of the current parent. this will keep the pistol correct.
you mean make an empty game object and make the player a child of that and keep the pistol a child of the player?
-EmptyGameObject --Player ---Pistol
Or do you mean have the player as its own object as it is now, but the sprite of the player should be a new object as a child of the player object, then flip that sprite and the pistol sprite individually?
Your answer
Follow this Question
Related Questions
2D rotate 90 degrees issue 1 Answer
Looking at target in 2D 1 Answer
Player rotation affecting Top Down 2D Movement 1 Answer
how to rotate 2d obj on android 1 Answer
Animation in Unity editor prevents rotation in script 1 Answer