- Home /
What would cause an objects transform to be different from its onscreen position?
In my game, I have a gun object which I am trying to get to shoot another bullet object. In order to instantiate the bullet object at the barrel of the gun instead of the center, I have another object placed at the barrel which I use as the position and rotation for my instantiate. This object is a child of the gun, so that as the gun rotates and moves, it will stay at the barrel. Upon running the game, I can see that the object stays at the barrel, even while the gun is moved and rotated. However when rotating the gun, the point at which the bullets are instantiated at does not change. Why is this happening and what could I do to fix it?
Answer by meat5000 · Apr 02, 2016 at 12:34 PM
Parenting and object will make the Transform information in the inspector show local instead of world space. It is relative to the parent and without the parent your object is parented to the world.
To a child, 0,0,0 represents the centre of the parent.
The Scene View issue is that the Handles you see in the window show the averaged centre of all the sub-objects in the hierarchy of each object. To test this, make a bunch of objects and child them to each other, deep as you like. Take the bottom-most child and move its position to something further away. Click back on the top-most parent and see where the handle has moved to.
When scripting it is important to take in to account world space and local space of a transform. The Transform class has several methods for dealing with converting from one to the other.
These are called TransformDirection, TransformPoint, TransformVector and their Inverses and now the Transform includes localToWorldMatrix and worldToLocalMatrix for use with shader parameters.
Your answer
Follow this Question
Related Questions
Network.Instatiate Problem 0 Answers
Problems with instantiating a clone as a child? 1 Answer
How Can I Make an Instantiated Object a Child of Other Object? 2 Answers
Delay child objects rotation 0 Answers