CREATE YOUR WORLD
Interacting with Objects
Implementing an object that follows the character
3min
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 open the navigation (obsolete) window in the unity top menu, click window > ai > navigation (obsolete) to open the navigation (obsolete) window define movable areas for the pet object 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 ✏️ for more details on navmesh, please refer to the video below bake the navmesh click the bake tab, then click the bake button to generate the navmesh 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 ✏️ for more details on navmeshagent, please refer to this link in this example, we set the navmeshagent’s stopping distance to 3, so the pet stops at a distance of 3 from the player 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 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 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 press the play button 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 ✏️ for more details on object attachment, please refer to attaching objects to zepeto characters docid\ rqti5gxrdenvblefpzzih guide 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 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 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 press play to see the pet object appear attached to the selected body part