Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by crowbar1 · Jan 13, 2016 at 06:54 PM · 2drotation2d rotationtop downparent and child

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.

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave29483 · Jan 15, 2016 at 08:18 AM 1
Share

I think you may need to update localRotation ins$$anonymous$$d of rotation.

avatar image crowbar1 Dave29483 · Jan 15, 2016 at 01:31 PM 0
Share

I didnt even knew localRotation existed. I checked out what it is in the Unity documentation, might work, will try. Thanks.

avatar image crowbar1 Dave29483 · Jan 15, 2016 at 01:57 PM 0
Share

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.

avatar image saschandroid · Jan 15, 2016 at 12:37 PM 0
Share

Have you tried to scale the parent (like scale.x *= -1) to flip it on the x-axis?

avatar image hexagonius saschandroid · Jan 15, 2016 at 12:40 PM 0
Share

that's a very bad idea. Never scale anything negative in Unity, you just get in trouble

avatar image saschandroid hexagonius · Jan 15, 2016 at 01:00 PM 0
Share

Ok. I never did 2D games ... I thought negative scaling of sprites in 2D is comparable to flipping textures in 3D.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image crowbar1 · Jan 15, 2016 at 01:29 PM 0
Share

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.

avatar image crowbar1 · Jan 15, 2016 at 01:56 PM 0
Share

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.

avatar image hexagonius crowbar1 · Jan 15, 2016 at 02:08 PM 0
Share

$$anonymous$$ake the Player visual also child and flip it ins$$anonymous$$d of the current parent. this will keep the pistol correct.

avatar image crowbar1 hexagonius · Jan 15, 2016 at 02:23 PM 0
Share

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?

Show more comments
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges