Studio GuideWorld SDK Guide
Log In

Attaching objects to ZEPETO characters

Attach an object to a ZEPETO character so that when the character moves, the object moves with it.

800

STEP 1 : Prepare an Object Prefab


First, you need to turn your object into Prefab.
Create an object, drag it to the Asset folder and make it a Prefab.

1080

Example of an Object Prefab


STEP 2 : Script to attach the object to a specific part of the character


  • Implement the ZEPETO character creation code in Scene as a default.

📘

Please refer to the following guide. [Create a ZEPETO Character]


STEP 2-1 : Write the script

  1. Create Hierarchy > Create Empty Object and rename it to AttachObject.
  2. Create Project > Create > ZEPETO > TypeScript and rename it to AttachObject.
  3. Write a sample script like below.
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { ZepetoCharacter, ZepetoPlayers } from 'ZEPETO.Character.Controller';
import { Transform, Animator, GameObject, HumanBodyBones, Object } from 'UnityEngine';

export default class AttachObject extends ZepetoScriptBehaviour {

    // The object prefab to be attached on the body.
    public prefItem: GameObject;
    // The bone to attach the object to.
    public bodyBone: HumanBodyBones;

    private _localCharacter: ZepetoCharacter;

    Start() {
      
        ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            // Find the local player and set it to _localCharacter.
            this._localCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
            // Get the _localCharacter's animator component.
            const animator: Animator = this._localCharacter.ZepetoAnimator;
            // Get the position of the bone to attach the object to.
            const bone: Transform = animator.GetBoneTransform(this.bodyBone);
            // Create the object prefab.
            Object.Instantiate(this.prefItem, bone) as GameObject;
          
        });
    }
}

  • The script flows as follows:
    • Start()
      • Register the ZepetoPlayers.instance.OnAddedLocalPlayer event listener, which will fire when a local player is added.
      • Set the local player to the _localCharacter variable.
      • Get the animator component of _localCharacter, get the location specified in bodyBone, and create the prefab specified in prefItem at that location.

STEP 2-2 : Setting the Attachment Location in the Inspector

  1. After finishing writing the script, add the script to the AttachObject object.
  2. In the Inspector, assign the Pref Item, Body Bone.
    • Pref Item is the object prefab.
    • Body Bone is the location where the object will be created.
      • Select LeftHand to make it look like you are holding the object in your hand.

844

Example Script Setting Screen


  1. Press the Play button to run and you will see the ZEPETO character with the object attached to his left hand.

STEP 3 : Apply


You can attach any object to the desired location of the Body Bone in the same way.

📘

Unity HumanBodyBones

https://docs.unity3d.com/ScriptReference/HumanBodyBones.html


The following is an example of attaching a Neck Pillow object from BuildIt to the character's neck to make it look like the ZEPETO character is wearing a neck pillow.

800
  • You can adjust the Position and Rotation of the object appropriately to get the desired look.
1378

Example of Object Prefab configuration


  • In the Inspector, assign the Neck Pillow object to the Pref Item, and select Neck for the Body Bone.
844

Example Script Setting Screen