Quantcast
Channel: Questions in topic: "parenting"
Viewing all articles
Browse latest Browse all 302

Trying to understand parenting

$
0
0
Hi all, I'm still trying to wrap my head around parenting a game object. I have a player object with a nested game object ( `gunContainer` )that has an animation I trigger on certain key inputs. When a user keys 1-4 on a keypad, in the Update method, I 1. instantiate the desired object `activeGun = Instantiate(guns[0], gunContainer.transform);` 2. trigger the animation when I first tried this, the new object was not at all where I expected it, so I changed a few things. Eventually, I fell on the following ... if (Input.GetKey(KeyCode.Alpha1) || Input.GetKey(KeyCode.Keypad1)) { if (activeGun == null) { activeGun = Instantiate(guns[0]); } } ...later in the update if (activeGun != null) { activeGun.transform.SetPositionAndRotation(gunContainer.transform.position, gunContainer.transform.rotation); } This works but I do not understand why I have to explicitly reset the position and rotation on every frame. My mind thought I could just drop in the object and let the parent do its animation magic. Too easy? Can someone explain to me why I have to match the parent transform and rotation every frame, where in my head I assumed as a child its transform and rotation would always be Vector3.zero / Quaternion.identity Thanks!

Viewing all articles
Browse latest Browse all 302

Trending Articles