Implementing an object that follows the character
You can use the NavMeshAgent component to implement AI objects that follow your character around.
In this guide, you will implement a Pet type object that follows you around.
1) Set up a navigation mesh (NavMesh) to define the area where the Pet object can move.
- Please refer to the NavMesh guide for details on how to set it up.
- Set the Navigation Static property of the Background object and press the Bake button on the Bake tab to generate the navigation mesh.
2) Add a NavMeshAgent component to the Pet object and set the size, rotation speed, acceleration, etc. of the Agent.
- Here, the Agent is the Pet object that uses the NavMesh's navigation system.
- By changing the values of the Agent, you can set the speed at which the Pet moves, the rotation speed and acceleration when it moves, the height at which it recognizes obstacles, and more.
- We set the Stopping Distance of the Nav Mesh Agent to 3 to make the Pet stop at a distance of 3 from the player.
- Please refer to the NavMeshAgent guide for details on how to set it up.
📘 Unity Navmesh Agent Document https://docs.unity3d.com/2021.3/Documentation/Manual/class-NavMeshAgent.html
1) create a new TypeScript and name it PetController as shown below, and apply the script to the Pet object.
Script Description
- At the beginning of the script, specify your ZEPETO character in _target.
- In the Update function, every frame, use the NavMeshAgent's SetDestination function to set my ZEPETO character's position to the NavMesh's target point.
2) Now press the Play button to execute and you will see that the Pet object will follow your character and move around obstacles like walls.
1) If you want to implement a pet that sits on top of the character's body instead of following the character, you can do so by attaching an object.
📘 Please refer to the following guide. [Attaching objects to ZEPETO characters]
2) Write the following AttachPetController script, where the variable Pet specifies the object that will be created as the pet.
- In the bodyBone tab, open the drop-down menu to select the part of the body where the Pet object will be attached.
Script Description
- When the script starts, it will find the local player and locate the character's animator.
- It will find which part of the body selected in the variable bodyBone is on the character's animator and locate it.
- Intantiate a pet object at the location we found and create it. You will parent the pet object to the body part of the character's animator that you found earlier.
3) Press the [▶︎(play)] button to run it and you should see the Pet object created on the selected body part, as shown in the image below.