---
title: Implementing an object that follows the character
slug: world-sdk-guide/implementing-an-object-that-follows-the-character
docTags: 
createdAt: 2024-09-02T08:34:13.135Z
---

Using the NavMeshAgent component, you can create an AI object that follows your ZEPETO character.

In this guide, we’ll create a Pet object that follows your character.

## Creating the Pet Object

:::::WorkflowBlock
:::WorkflowBlockItem
**Open the Navigation (Obsolete) Window.**

![](https://api.archbee.com/api/optimize/xv-TGeWVTX2GDZMFGOf1Y/ioeagnYkRZnNx3-Fh3DQH_image.png)

In the Unity top menu, click **Window > AI > Navigation (Obsolete)** to open the Navigation (Obsolete) window.
:::

::::WorkflowBlockItem
**Define movable areas for the Pet object.**

::Image[]{src="https://api.archbee.com/api/optimize/xv-TGeWVTX2GDZMFGOf1Y/6Hnn9ZZKQBMr-JhQhFlt2_image.png" size="70" width="710" height="458" position="center" showCaption="false"}

In the Navigation (Obsolete) window, click the **Object** tab. From the Hierarchy, select floors, walls, and obstacles that will be included in the NavMesh, and add them as Objects.

- This sets them as navigable surfaces or obstacles for the Pet object.

Make sure the **Navigation Static** field is checked.

:::hint{type="info"}
✏️ For more details on NavMesh, please refer to the video below.

::embed[]{url="https://www.youtube.com/watch?v=mP7ulMu5UkU"}
:::
::::

:::WorkflowBlockItem
**Bake the NavMesh.**

![](https://api.archbee.com/api/optimize/xv-TGeWVTX2GDZMFGOf1Y/Q-90_MdXrB1zN3pc8dzIj_image.png)

Click the **Bake** tab, then click the **Bake** button to generate the NavMesh.
:::

::::WorkflowBlockItem
**Create a NavMeshAgent.**

Select the Pet object in the Hierarchy. In the Inspector, add the **NavMeshAgent** component.

- An Agent refers to the Pet object navigating through the NavMesh system.

You can adjust attributes like size, speed, rotation speed, acceleration, and obstacle height recognition.

:::hint{type="info"}
✏️ For more details on NavMeshAgent, please refer to this [link](https://docs.unity3d.com/550/Documentation/Manual/class-NavMeshAgent.html).
:::

![](https://api.archbee.com/api/optimize/xv-TGeWVTX2GDZMFGOf1Y/NWdi4SSnexNRcoVeE13jG_image.png)

In this example, we set the NavMeshAgent’s Stopping Distance to 3, so the Pet stops at a distance of 3 from the player.
::::

:::WorkflowBlockItem
**Create a PetController script.**

In the Project window, click **+ button > ZEPETO > TypeScript** to create a new Zepeto script and rename it to **PetController**.

Then open the script and copy-paste the code below.

```typescript
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 Explanation

- When the script starts, the \_target is assigned as your ZEPETO character.
- In the Update function, every frame the NavMeshAgent’s SetDestination function sets your character’s position as the navigation target.
:::

:::WorkflowBlockItem
**Apply PetController script to Pet object.**

Select the Pet object in the Hierarchy. In the Inspector, click **Add Component > Zepeto Script**, then drag and drop the created PetController script into the Script field.
:::

:::WorkflowBlockItem
**Press the Play button.**

![](https://api.archbee.com/api/optimize/xv-TGeWVTX2GDZMFGOf1Y/yq6fVGzeb3eN12XefOpQe_petexample.gif)

You can now see the Pet object following your character while avoiding obstacles like walls.
:::
:::::



# Creating a Pet on Character’s Head

If you want the Pet to be attached to your character’s body rather than following, you can use object attachment.

:::hint{type="info"}
✏️ For more details on object attachment, please refer to [Attaching objects to ZEPETO characters](docId\:rqti5gXrDEnvBLEFPzzIh) guide.
:::



::::WorkflowBlock
:::WorkflowBlockItem
**Create an AttachPetController script.**

In the Project window, click **+ button > ZEPETO > TypeScript** to create a new Zepeto script and rename it **AttachPetController**.

Open the script and copy-paste the code below.

```typescript
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 Explanation

- When the script starts, it finds the local player and its animator component.
- It identifies the body part selected in the bodyBone variable within the character’s animator.
- Then, it instantiates the pet object at the identified position, setting the pet object’s parent as the character’s body part.
:::

:::WorkflowBlockItem
**Apply AttachPetController script to a new object.**

In the Hierarchy, click **+ button > Create Empty** to create a new object and rename it **AttachPetController**.

In the Inspector, click **Add Component > Zepeto Script** and drag-and-drop the AttachPetController script to the Script field.

Drag-and-drop the Pet object into the Pet field. Open the dropdown menu in the bodyBone field and select the character’s body part to attach the Pet object.

![](https://api.archbee.com/api/optimize/xv-TGeWVTX2GDZMFGOf1Y/nyLMD6vjoovCAoxHTe8OC_image.png)

Press Play to see the Pet object appear attached to the selected body part.
:::
::::

