Parent and Child Game Objects in Unity
Its time to put your baby toys and milk bottle down, we are not taking care of tiny little gameObject babies anymore. Instead, we're gonna learn how to create child gameObjects of main gameObjects in the Hierarchy, a technique that proves to be quite useful when used with the Instantiate()
method, that we have just learned about.
If you have directly arrived on this tutorial, we recommend you to check our last tutorial for understanding the ongoing example.
If you pay close attention to the character we've created in the previous tutorial, you will notice that the fireballs just spawn out of the middle of our main game character, not from where you would expect (Like in this case, the outstretched hand).
Now, what if instead of the character, there was an invisible gameObject that shot the fireballs? After all, a user (game player) wouldn't really be able to see who exactly shot the fireball. So let's do that. First of all, create a new empty gameObject.
You see, gameObjects in general have their location relative to the game world.
But for certain objects, like a gun or a car's horn, you might need it to be relative to another gameObject instead. Instead of saying This gameObject is 2 units away from the dead center of the game, we can make gameObjects say This gameObject is 2 units away from another gameObject, no matter where it is.
Let's try it out in our game, to see how it really works. To make a gameObject a child, simply drag and drop the intended child onto the intended parent gameObject. In our case, we'll drag our empty gameObject onto our character object (Shooting Savi).
You'll notice that the new gameObject now comes in a dropdown list under our main character, and has a small indentation added to its name in the Hierarchy. This means that the gameObject is now a child of our character. If you try to move the character now, you'll notice that the empty gameObject also moves with it, but remains at the same relative distance.
But the empty gameObject is floating near our character's head, and not near his hand! To fix that, simply adjust the position of the empty gameObject using the position
values in the Transform view, until the empty gameObject gets to where you need it to be.
Once again, a child gameObject will have its position relative to its parent, not the game world. Meaning if you set the position to (0, 0, 0)
, a child gameObject will get positioned in the dead center of the parent gameObject, not the screen.
In our case, if you remember from the last tutorial, we added the code the instantiate a new fireball everytime the spacebar is hit, in the script attached to Shooting Savi game object. And the same script also holds the code for enabling the Shooting Savi game object to move.
So we will have to remove the fireball prefab creation code from the update()
method of the Shooter
class.
Let's create a new class ShootingBehaviour
and add the code to the update()
method of this class and attach this script to the new Invisible child game object of the Shooting Savi game object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootingBehaviour : MonoBehaviour
{
public GameObject fireball;
void update()
{
// When spacebar is hit
if(Input.GetKeyDown(KeyCode.Space))
{
// instantiate the fireball object
Instantiate(fireball,
new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 0),
new Quaternion(0, 0, 0, 0));
}
}
}
And the Shooter
class which is making the Shooting Savi move will look like,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooter : MonoBehaviour
{
public RigidBody2D body;
public float speed;
void update()
{
body.velocity = new Vector2(Input.GetAxisRaw("Horizontal")*speed, Input.GetAxisRaw("Vertical")*speed);
}
}
Now that we've attached the shooting script to the empty gameObject and made it a child of our character's gameObject, let's test our game now.
It works! So what have we learned in this lesson? We've learned how gameObject parenting works and how we can use it to our advantage, and we didn't have to change a single dirty diaper (we don't have to do much work).