Studio GuideWorld SDK Guide
Log In

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.


STEP 1 : Setting up the NavMesh

  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.

📘

Unity Navmesh Tutorial

https://www.youtube.com/watch?v=mP7ulMu5UkU

  • Set the Navigation Static property of the Background object and press the Bake button on the Bake tab to generate the navigation mesh.


  1. 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/kr/2021.3/Manual/class-NavMeshAgent.html


STEP 2 : To write a Pet AI script

  1. create a new TypeScript and name it PetController as shown below, and apply the script to the Pet object.
import { Transform } from 'UnityEngine'
import { NavMeshAgent } from 'UnityEngine.AI';
import { ZepetoPlayers } from 'ZEPETO.Character.Controller'
import { ZepetoScriptBehaviour } from 'ZEPETO.Script'
import { WorldService } from 'ZEPETO.World';
 
export default class PetController extends ZepetoScriptBehaviour {
 
    private _target: Transform;
    private _navMeshAgent: NavMeshAgent;
 
    Start() {
        this._navMeshAgent = this.GetComponent<NavMeshAgent>();
 
        ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            this._target = ZepetoPlayers.instance.GetPlayer(WorldService.userId).character.transform;
        })
    }
 
    Update() {
        if (this._target == null) {
            return;
        }
        this._navMeshAgent.SetDestination(this._target.position);
    }
}

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.

  1. 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.


Creating a pet above the character's head

  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]


  1. 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.
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { ZepetoCharacter, ZepetoPlayers } from 'ZEPETO.Character.Controller';
import { Transform, Animator, GameObject, HumanBodyBones, Object } from 'UnityEngine';
 
export default class AttachPetController extends ZepetoScriptBehaviour {
 
    public Pet: GameObject;
    public bodyBone: HumanBodyBones;
 
    Start() {
 
        ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            const localCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;           
            const animator: Animator = localCharacter.ZepetoAnimator;           
            const bone: Transform = animator.GetBoneTransform(this.bodyBone);           
            Object.Instantiate(this.Pet, bone) as GameObject;         
        });
    }
}

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.

  1. 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.