官术网_书友最值得收藏!

Object hierarchies

Some complex objects may need to be separated in sub-objects, each one with its own components. Those sub-objects need to be somehow attached to the main object and work together to create the necessary object behavior.

In this section, we will cover the following concepts related to components:

  • Parenting of objects
  • Possible uses

Parenting of objects

Parenting consists of making an object the child of another, meaning that those objects will be related to each other. One type of relationship that happens is a Transform relationship, meaning that a child object will be affected by the parent's Transform. In simple terms, the child object will follow the parent, as if it is attached to it. In an example, imagine a player with a hat on their head. The hat can be a child of the player's head, making the hat follow the head while they are attached.

In order to try this, let's create a capsule that represents an enemy and a cube that represents the weapon of the enemy. Remember that in order to do, so you can use the GameObject | 3D Object | Capsule and Box options. An example capsule can be seen in the following screenshot:

Figure 3.23 – A capsule representing a human and box representing a weapon

If you move the enemy object (the capsule), the weapon (the box) will keep its position, not following our enemy. So, in order to prevent that, we can simply drag the weapon to the enemy object in the Hierarchy window, as illustrated in the following screenshot:

Figure 3.24 – Parenting the cube weapon to the capsule character

Now, if you move the enemy, you will see the gun moving, rotating, and being scaled along with it. So, basically, the gun transform also has the effects of the enemy Transform component.

Now that we have done some basic parenting, let's explore other possible uses.

Possible uses

There are some other uses of parenting aside from creating complex objects. Another common usage for it is to organize the project Hierarchy. Right now, our scene is simple, but in time it will grow, so keeping track of all the objects will become difficult. So, to prevent this, we can create empty GameObjects (in GameObject | Create Empty) to act as containers, putting objects into them just to organize our scene. Try to use this with caution, because this has a performance cost if you abuse it. Generally, having one or two levels of parenting when organizing a scene is fine, but more than that can have a performance hit. Consider that you can—and will—have deeper parenting for the creation of complex objects; the proposed limit is just for scene organization.

To keep improving on our previous example, duplicate the enemy a couple of times all around the scene, create an empty Game Object called Enemies, and drag all the enemies into it so that it will act as a container. This is illustrated in the following screenshot:

Figure 3.25 – Grouping enemies in a parent object

Another common usage of parenting is to change the pivot or center of an object. Right now, if we try to rotate our gun with the Transform Gizmo, it will rotate around its center because the creator of that cube decided to put the center there. Normally, that's okay, but let's consider the case where we need to make the weapon aim at the point where our enemy is looking. In this case, we need to rotate the weapon around the weapon handle; so, in the case of this "box" weapon, it would be the closest end to the enemy. The problem here is that we cannot change the center of an object, so one solution would be to create another "weapon" 3D model or mesh with another center, which will lead to lots of duplicated versions of the weapon if we consider other possible gameplay requirements such as a rotating weapon pickup. We can fix this easily using parenting.

The idea is to create an empty GameObject and locate it where we want the new pivot of our object to be. After that, we can simply drag our weapon inside this empty GameObject, and, from now on, consider the empty object as the actual weapon. If you rotate or scale this weapon container, you will see that the weapon mesh will apply those transformations around this container, so we can say the pivot of the weapon has changed (actually, it hasn't, but our container simulates the change). The process is illustrated in the following screenshot:

Figure 3.26 – Changing the weapon pivot

Now, let's continue seeing different ways of managing GameObjects, using Prefabs this time.

主站蜘蛛池模板: 无锡市| 宽甸| 永昌县| 新丰县| 车险| 丰城市| 紫云| 吉首市| 高碑店市| 淄博市| 广河县| 潢川县| 沾益县| 邯郸县| 江阴市| 怀集县| 三穗县| 武安市| 岢岚县| 来凤县| 重庆市| 博湖县| 盱眙县| 斗六市| 曲麻莱县| 梧州市| 商城县| 集安市| 城步| 商丘市| 鄯善县| 宜都市| 清水河县| 牙克石市| 泾阳县| 乐亭县| 罗田县| 温泉县| 湖口县| 肥城市| 泸州市|