• 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
1
Question by CrucibleLab · Mar 10, 2015 at 11:11 PM · rotationaxishierarchycannon

Child gameObject won't stick to Parent upon rotation

Folks, I created a rudimentary cannon object with a simple hierarchy as follows:

alt text

Using the arrow keys, I can rotate the Gun Base left and right around the Y-axis and rotate the Gun up and down around the X-axis. All the rotation seem to work correctly, but I noticed that my projectiles shoot from inaccurate FiringPoint when the Gun is tilted at a steep angle.

In its initial state, the FiringPoint empty gameObject is at the correct position (This is in the X-axis perspective; the Gun is at the top and the Gun Base is at the bottom). As you can see, the FiringPoint gradually starts lagging behind as the Gun starts tilting upward.

alt text

I fiddled with the axis for quite a bit, but can't seem to figure out how to correct this behavior. I have been away from Unity for a long time, so please don't be too harsh with your comments. :)

EDIT 1:

With further troubleshooting, I found out that the 'Global' and 'Local' toggle button found just below the Unity main menu results in different behaviors for the FiringPoint.

When Global is selected, its Z-axis stays aligned with the initial direction (as shown in the screen captures), but when Local is selected, the Z-axis points to the same direction to which the Gun is pointing to. While the latter result seems to be more desirable, the center of the 'FiringPoint' is still off from the intended location, which is at the tip of the Gun.

EDIT 2:

To further elaborate the object setup, I have two separate scripts that are being used in this scene. First script for rotation around the Y-axis is attached to the Gun Base, and the other script for rotation around the X-axis is attached to the Gun Mount Point. There's probably a better way to achieve the same result and if there is, I'd like to know!

One other caveat I noticed is that the Gun object itself gets heavily skewed when I test the up & down, left & right rotation. It doesn't happen all the time, but it usually happens when I tilt up the Gun to almost 90 degrees, and then rotate it either to the right or left. And the object remains skewed until I reset the Play mode. Very odd...

cannon.jpg (18.1 kB)
hierarchy.jpg (8.9 kB)
Comment
Add comment · Show 15
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 Glurth · Mar 11, 2015 at 02:33 AM 0
Share

It looks like you are actually rotating the "gun mount point" rather that the gun: is this correct?

EDIT: Ah. based on picture 1, I see the gun and base are parallel- so I guess you ARE rotating the gun. Try rotating the "gun mount point" transform, this way, one end of the gun (the one on the mount point), will always be attached to the base.

avatar image Baste · Mar 11, 2015 at 08:44 AM 0
Share

"With further troubleshooting, I found out that the 'Global' and 'Local' toggle button found just below the Unity main menu results in different behaviors for the FiringPoint."

Those will change the behavior of the scene view gizmo - they won't change the position of the actual object. If your firing point is attached to the gun where you're showing it, it will stay there when rotating the gun. Try to make a temporary graphical marker at the firing point.

avatar image CrucibleLab · Mar 11, 2015 at 01:21 PM 0
Share

@glurth - I'm actually rotating the Gun $$anonymous$$ount Point. I attached a script to that specific object to address the Up & Down arrow keys.

avatar image CrucibleLab · Mar 11, 2015 at 01:26 PM 0
Share

@baste - Not sure if I understand what you're saying 100%, but I initially moved the 'FiringPoint' to that specific location (near the one end of the Gun barrel), and then I assigned it as a child to the Gun object, which I thought would make the FiringPoint move along the movement of Gun object.

avatar image Glurth · Mar 11, 2015 at 02:26 PM 0
Share

I'm actually rotating the Gun $$anonymous$$ount Point

ok perfect

I initially moved the 'FiringPoint' to that specific location (near the one end of the Gun barrel), and then I assigned it as a child to the Gun object

This sounds correct to me.

I think what Baste was talking about was how/where the transform gizmo (the arrows you have showing the firing point) is displayed. I think he wants you to make the firing point a cube, or sphere, or something for visualization. Personally I'd be surprised to find those gizmo's anywhere but the center of any cube or sphere you add, but I also don't doubt Baste. (hmm, don't use a sphere- orientation will not be apparent)

avatar image Baste · Mar 11, 2015 at 03:01 PM 0
Share

If you have an object without any renderer on it, the transform gizmo tends to be placed in funny places. If you have selected an empty parent object with a bunch of children with renderers, the gizmo is placed at the center of the child objects, if you have the rotation settings set to the default "center" setting.

I was thinking that this might be the issue. @Crucible, the suggestion is as @Glurth is saying to create a temporary object for visualization, and see if the problem is actually a rotation problem, or just the gizmos being wonky.

avatar image Glurth · Mar 11, 2015 at 03:08 PM 0
Share

oh gosh, that is useful to know! Thanks Baste!

avatar image CrucibleLab · Mar 11, 2015 at 03:16 PM 0
Share

@baste & @glurth - Thanks for the helpful tip. I'm currently at work, so I will try out the suggestion as soon as I get a chance and will let you know!

avatar image CrucibleLab · Mar 11, 2015 at 09:24 PM 0
Share

@baste & @glurth - I made a quick change per your suggestion and temporarily replaced the FiringPoint with a cube. I also assigned the same script to the cube. The only other difference is that I unchecked the box collider just so the projectiles do not collide with the cube. Sadly, I got the same result but I noticed that there's the green boundary around the cube that stretches as the angle of the Gun increases from its initial angle:

alt text

I'm starting to wonder if I screwed something up with the hierarchy. This shouldn't be this hard... :(

cannon1.jpg (8.1 kB)
avatar image Glurth · Mar 11, 2015 at 09:59 PM 0
Share

I also assigned the same script to the cube

This should not be necessary and will add some extra rotations to the cube.

As a sanity check, make the cube a child of the mount point. When you do this, it should rotate around the mount point, just like the barrel.

(The reason you don't want to leave it like this after your test is: so you can, for example, scale the cannon barrel to make it longer, and have the firing point stay at the end.)

FYI: Rather than use a script, you can always drag around the transform rotations right in the inspector of the mount point- you don't even need to launch the game to see the effects. (in fact, this test would eli$$anonymous$$ate any possible script errors)

avatar image CrucibleLab · Mar 11, 2015 at 10:20 PM 0
Share

@glurth - The script I assigned to the FiringPoint is only for shooting the projectiles and it has nothing to do with rotation, so I just left it be. So, I re-assigned the FiringPoint to the Gun $$anonymous$$ount Point (so it's now a sibling of Gun), but the result is about the same.

Another odd thing happens when I rotate the Gun $$anonymous$$ount Point by manually changing the values in the Inspector:

alt text

Yep, the Gun object gets totally skewed. I have no idea why.

cannon2.jpg (13.0 kB)
avatar image Baste · Mar 11, 2015 at 10:42 PM 0
Share

Does this still happen if you deactivate all of the scripts? What are those scripts doing? The end point should not be moving like that, so I'm feeling that it might be the scripts that are interfering. Colliders shouldn't matter - objects should not collide with their parent objects. That is, unless you have rigidbodies both on the parents and the children, which is usually not something you should do.

Also, look at the values in the inspector of the thing you're rotating when you rotate it. Is it rotating on more than one axis? Try to rotate the object through setting the rotation values in the inspector, that might do something different.

avatar image CrucibleLab · Mar 12, 2015 at 02:56 PM 0
Share

@glurth - You nailed it! The main thing I didn't realize is that modifying the Scale values of the parent object will result in unintended consequences for the movement of its children objects.

I still wonder if there's a workaround to this constraint (It'd still be nice to be able to change the shape of the parent object without messing up the orientation of children!), but this is good enough for now.

Please copy and paste your last comment in the answer section just so you get the credit you deserve. :)

avatar image Baste · Mar 12, 2015 at 05:22 PM 0
Share

I converted @glurth's comment to an answer and marked it as correct for you.

$$anonymous$$oderator privileges!

avatar image Glurth · Mar 12, 2015 at 05:44 PM 0
Share

@Baste thank you :)

I'll edit it up to be an answer rather than a comment

I still wonder if there's a workaround to this constraint

A bit complex but here goes: You COULD create a "$$anonymous$$ESH transform" component. This component, on Start() can modify the vertices that make up an object's mesh, by applying a supplied transform. This transform, applied only once, would effect ONLY this object's mesh. This transform would NOT be changeable at run-time (well it would; just might be slow to re-apply it to each vertex in the mesh)

1 Reply

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

Answer by Glurth · Mar 11, 2015 at 11:08 PM

Unchild the mount point from the base.

I don't see a "rotate base point" object in the heirarchy, but both the base, and the mount point would be children of this, "empty" object.

The reason for this: each parent transform is applied in order, from the "childmost" child on up. So the way you had it, the scale part of the "BASE" object's transform was being applied to your "mount-point's" rotation transform. (Note, it was also effecting the size/shape of your gun-barrel).

Order is particularly important when applying transforms. Applying an uneven scaling transform after a rotation transform is generally avoided, as it leads to odd/non-intuitive results. (well, some people can intuit it, not me) An example of such results can be seen in the comments section, where the gun barrel becomes "skewed".

alt text

There is no way, that I know of, in unity to apply a transform to "only this object", and not have it apply to it's children. To keep the system consistent, to do something like this would require a SECOND set of transform data (need one for each: "this object only", "children affecting"), which is effectively, what the empty object we created provide.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

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

Related Questions

Spinning a sphere, like a globe. 1 Answer

90 Degree stopping rotation on y-axis issue 0 Answers

LookAt on one axis 2 Answers

Transform.Translate but ignore rotation on one axis 0 Answers

Orbit Collision Axis 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