Child gameObject won't stick to Parent upon rotation

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

42279-hierarchy.jpg

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.

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. :slight_smile:

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…

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.

"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.

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

@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.

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.

1 Answer

1

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.