Attach an object to a ZEPETO character so that when the character moves, the object moves with it.
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.
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
- Create Hierarchy > Create Empty Object and rename it to AttachObject.
- Create Project > Create > ZEPETO > TypeScript and rename it to AttachObject.
- 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.
- Start()
STEP 2-2 : Setting the Attachment Location in the Inspector
- After finishing writing the script, add the script to the AttachObject object.
- 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.
- 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.
- You can adjust the Position and Rotation of the object appropriately to get the desired look.
- In the Inspector, assign the Neck Pillow object to the Pref Item, and select Neck for the Body Bone.