Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 maradine · Jan 02, 2015 at 08:47 PM · rotationparentchildrotation axisparent-child

Why is a child object not rotating properly with parent?

The question:

Why is a child object accepting it's parent rotations when it itself is motionless, but seeming to refuse it when rotating itself?

The setup:

Assume 3 transforms in a hierarchy. (A) is the parent of (B), (B) is the parent of (C).

(A) has a script attached to it that controls movement of the collective. It has a section inside FixedUpdate() that translates input into rotation:

 //handle body yaw and tilt calculations
 float yawMag = rotationSpeed * Time.deltaTime * Input.GetAxis ("BodyYaw");
 //this actually rotates the main transform
 transform.Rotate (Vector3.up, yawMag);

There is also a section a little later that simulates inertia and tilts the transform a bit as yaw and speed increases.

 transform.localEulerAngles = new Vector3(0, transform.rotation.eulerAngles.y, balanceRotationLimitDegrees*balancerLoad);

(B) is a pivot for (C), which is the actual camera. (B) has a script with code in FixedUpdate() like this to handle and limit rotation:

 float trot = Input.GetAxis("Camera Traverse") * Time.deltaTime * traverseRate;
 transform.Rotate(transform.up, trot);
 float angleOffBow = Quaternion.Angle(transform.parent.rotation, transform.rotation);
 //Debug.Log("AoB: " + angleOffBow);
 if (angleOffBow > traverseLimit) {
     transform.Rotate(transform.up, -trot);
     Debug.Log("Bounced off traverse limiter");
 }
 
 float erot = Input.GetAxis ("Camera Elevation") * Time.deltaTime * elevationRate;
 childCamTransform.Rotate(Vector3.right, erot, Space.Self);
 angleOffBow = Quaternion.Angle(childCamTransform.rotation, transform.rotation);
 //Debug.Log("AoB: " + angleOffBow);
 if (angleOffBow > elevationLimit) {
     childCamTransform.Rotate(Vector3.right, -erot, Space.Self);
     Debug.Log("Bounced off elevation limiter");
 }

When running around and yawing (A), everything works as expected. The camera pivot (B) is appropriately rolled with the main body and comes back when (A) comes back to full vertical. The camera itself (C) also functions properly, yawing and elevating within its limits. When these two things combine, however, things go wrong. Rotating (B) while (A) is tilted results in some sort of permanent local Z axis rotation and (B) comes back up with its axes screwed up. I've tried a bunch of different iterations of different ways of rotating things and referring to axes, but I'm missing something fundamental here.

Any thoughts?

EDIT: Fixed it. Took the instances of

 transform.Rotate(transform.up, trot);

and changed them to

 transform.Rotate(Vector3.up, trot, Space.Self);

I was under the impression that these two things are essentially the same = the Vector3.up vector in self space is essentially the transform's up axis. Clearly I'm wrong here so, the question answer criteria is now "can someone tell me why/how these things aren't the same?"

Thanks!

Comment
Add comment · Show 3
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 _dns_ · Jan 02, 2015 at 10:22 PM 0
Share

Hi, you use multiple objects to simplify the rotations of the camera and that's usually the best way to do it. When coding this kind of script, I found it easier to manage the angles values myself and feed them through the localRotation of the object's transforms. I.$$anonymous$$ define a "private float TRot" in B class (and ERot) and setting the angles of your B object with localRotation = Quaternion.Euler(ERot, TRot, 0.0f). That way, all local angles are known and Unity will combine those rotations when computing the transform hierarchy (it's also easier to debug).

I'm not familiar with transform.Rotate but I suspect it may use the existing hierarchy to compute the rotation, using the parent's rotation in the equation. This may introduce some little rotations that accumulate. $$anonymous$$aybe if you do all the rotations in the Space.self like you do for some, it may fix it.

avatar image maradine · Jan 03, 2015 at 01:08 AM 0
Share

$$anonymous$$y understanding of transform.Rotate is that it operates entirely in Quaternion space and only local to the rotation itself, but I could definitely be wrong. I'll be messing with this while waiting for someone to show up with a "gotcha".

EDIT: fixed above. Want to take a swing?

avatar image _dns_ · Jan 03, 2015 at 01:59 AM 0
Share

transform.up is rotated by the object's transform but Vector3.up is not (And I've checked, the default value for the "Space" is Space.Self so this doesn't make a difference, I should have checked this in my previous comment :-)

So, transform.up will take in account the parent's rotation while Vector3.up won't (= always (0,1,0)). It explains why you had some sort of "permanent" rotation that adds up (transform.up = Vector3.up rotated by the parent's transform)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by maradine · Jan 03, 2015 at 02:13 AM

Can you convert that to an answer so I can give you credit?

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why does the child of a game object not rotate to face mouse position when the parent object is moving? 1 Answer

Make a simple tree 1 Answer

First Person Camera not being child of the character/ Set child's rotation to global rotation 2 Answers

How To Get List of Child Game Objects 14 Answers

Transform.rotation is setting local rotatoin 0 Answers


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