- Home /
How to make a child game object move based on the parent position ?
So i want to make the turret of a tank and a CHILD of it, go back and forth when shooting, so this is what i've done :
IEnumerator Gun1BackandForth()
{
// when i shoot i call this Coroutine to move the position
//of the tank on the y and then return it ( to tank position) :
Vector3 position = Gun1Transform.position;
position.y -= 0.1f;
Gun1Transform.position = position;
yield return new WaitForSeconds(0.05f);
Gun1Transform.position = transform.position;
}
it works great ! but when the tank is rotated, the turret keeps go DOWN, like it's not a child of the tank, it does follow the tank but when shooting and reducing the y value, the turret goes down always. isn't the turret suppesed to have new coordinations because it's a child? please help !
You have a video or something. I don't understand what you mean. :(
could you give more detail on what you're intending to happen here? reducing the y value is always going to move an object down.
if you want a transform relative to an object, you want to use transform.localPosition
, though I can't see that changing anything from what I can see in the code
Answer by satish004004 · Aug 03, 2019 at 02:06 PM
childTargetToPosition= targetposition - child.position;
childTargetToPosition.y = 0; //Assumption y position whichever you want to
parent.position = parent.position + childTargetToPosition;
Answer by MounirDev · Aug 03, 2019 at 10:27 PM
The problem actually was in transform.position, this is the global transform, so i need to replace it by transform. localPosition instead. I hope this helps someone ! If you still a bit puzzled search for global and local positions !
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
How can I make a game object follow an instantiated game object? 1 Answer
Set Position and Rotation with another GameObject as Pivot (Script) 0 Answers
how to do full child-parent transformation, without parenting? 2 Answers
Need camera to follow player, but not the player's rotation. 13 Answers