CREATE YOUR WORLD
Players & Characters: Advanced

Creating an NPC

36min

Learn how to create NPCs from ZEPETO Characters.

Apply the sample scripts provided for each feature to add the NPC of your choice to your World.



Create an NPC using a ZEPETO ID.

Document image




  • The NPC's appearance and items worn will be the same as the ZEPETO ID entered.
  • It is recommended that you set up a ZEPETO character with a specific appearance before creating an NPC.



Setting the location object where the NPC will be created



  • Implement the ZEPETO character creation code in your Scene by default.

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



  • Create Hierarchy > Create Empty Object and rename it to NPC.
    • An object to store the location where the NPC will be created.
    • Set the Position, Rotation.
Document image




Writing an NPC creation script



1) Create Project > Create > ZEPETO > TypeScript and rename it to NPCCreator.

2) Write a sample script like below.

TypeScript




The flow of the script is as follows:

  • Start()
    • Create an NPC in the NPC object location using the ZepetoCharacterCreator.CreateByZepetoId() function and save it in _npc.



3) After you have finished writing the script, add the script to the NPC object.

4) Assign the Zepeto Id in the inspector.

  • Zepeto Id : The ZEPETO ID of the NPC.
Example Script Setting Screen
Example Script Setting Screen




5) Press the play button to execute and the NPC will be created.

6) Add an NPC location object in the same way, and add an NPC script to create multiple NPCs easily.

Document image




Labeling NPC



You can label your NPC with name tags to distinguish them from players.

Document image




To create the Name Tag Canvas Prefab



1) Create a Hierachy > UI > Canvas and rename it PrefNameTagCanvas.

  • Set the Render Mode to World Space.
Example of Canvas Settings
Example of Canvas Settings




2) Create Hierarchy> UI > Text as a child of PrefNameTagCanvas and rename it to NameTagText.

  • The text to represent the name.
  • Add a Content Size Fitter component to make the text the right size.
Example of Text Settings
Example of Text Settings




3) Once you're done, drag it to the Project window to make it a Prefab, and then delete the PrefNameTagCanvas that's still in the highlighter.

Example of Prefab Configuration
Example of Prefab Configuration




Creating NPC Name Tag Script



1) Create a Project > Create > ZEPETO > TypeScript and rename it to NPCCreatorWithNameTag.

2) Write a sample script as shown below.

TypeScript




The flow of the script is as follows:

  • Start()
    • Call the SetNameTag() custom function.
  • SetNameTag()
    • Dynamically generates a name tag for the NPC and adjusts the position of the generated name tag above the NPC's head
    • Set the text inside the name tag.
  • Update()
    • Call the UpdateCanvasRotation() custom function to rotate the canvas to match the camera.



3) After you have finished writing the script, add it to the NPC object.

4) In the Inspector, assign the Zepeto Id, Name Tag, Name Tag Prefab, and Name Tag Y Offset.

  • Name Tag: The name that will appear on the NPC's name tag.
  • Name Tag Prefab: The name tag canvas prefab.
  • Name Tag Y Offset: A variable that stores the y-axis offset value for the Name Tag Canvas object. When you place the name tag above the character's head, you can adjust the distance between the character and the name tag.
Example Script Setting Screen
Example Script Setting Screen




5) press the play button to execute, and the NPC with the name tag will be created.

Document image




Controlling NPC Behavior



You can control the actions of NPC.



Jumping



Document image




Use the ZEPETO Character API to make the NPC jump.

You can use the ZEPETO Character API to implement a wider range of behaviors.

📘 Please refer to the following guide. [ZEPETO Character]



Creating an NPC Jump script

1) Go to Project > Create > ZEPETO > Create TypeScript and rename it to NPCJump.

2) Write a sample script as shown below.

TypeScript


The flow of the script is as follows:

  • Start()
    • Call the JumpCoroutine() coroutine.
  • JumpCoroutine()
    • Call the Jump() method to make the NPC character jump every 5 seconds.



3) After you have finished writing the script, add the script to the NPC object.

4) Press the Play button and the NPC will jump.



Gesture



Document image




Use the Animator Controller to implement gestures for NPC.

You can use the Animator Controller to implement a wider variety of behaviors.



Preparing your animation clip



  • Use the following guide to prepare the gesture animation clip for your NPC to perform.

📘 Please refer to the following guide.



Creating an Animator



1) Project > Create > Create Animator Controller and rename it to NPCAnimatorController.

Document image




2) On the Animator tab, Create State > Empty.

Document image




3) In the Inspector, rename it appropriately and assign the animation clip to Motion.

Document image




Creating an NPC Gesture Script



1) Create a Project > Create > ZEPETO > TypeScript and rename it to NPCGesture.

2) Write a sample script as shown below.

TypeScript




The flow of the script is as follows:

  • Start()
    • Get the Animator component of the NPC object and set it to the Animator Controller specified by the npcAnimator variable.



3) After you finish writing the script, add it to the Location object where the NPC will be created.

4) In the Inspector, assign the Zepeto Id, Npc Animator.

  • Npc Animator: The NPC's animation controller.
Document image




5) Press the play button to run it and you'll see the NPC make a gesture.

6) You can apply this to create NPCs that do more actions besides gestures and jumps.



Speech bubbles over NPC



You can create a Canvas above an NPC's head, display an image or text, and make it bubble.

Document image




Creating a Speech Bubble Canvas Prefab

1) Create a Hierarchy> UI > Canvas and rename it to PrefSpeechBubbleCanvas.

  • Set the Render Mode to World Space.
Document image




2) Create a Hierarchy> UI > Image as a child of PrefSpeechBubbleCanvas and rename it to SpeechBubbleImage.

  • This is the image that will be the background of the speech bubble.
Example of Image Settings
Example of Image Settings




3) Create Hierarchy> UI > Text as a child of SpeeachBubbleImage and rename it to SpeeachBubbleText.

  • This is the text inside the speech bubble.
  • Add a Content Size Fitter component to make the text the right size.
Example of Text Settings
Example of Text Settings




4) Once you're done, drag it to the Project window to make it a Prefab, and then delete the PrefSpeechBubbleCanvas that is still in the Highlight.

Example of Prefab Configuration
Example of Prefab Configuration




Creating NPC Speech Bubble Script



1) Create a Project > Create > ZEPETO > TypeScript and rename it to NPCSpeechBubble.

2) Write a sample script as shown below.

TypeScript




The flow of the script is as follows:

  • Start()
    • Call the SetBubble() custom function.
  • SetBubble()
    • Create a speech bubble canvas (speechBubblePrefab) and position the created speech bubble above the NPC's head
    • Call the SetBubbleText() custom function to set the text inside the speech bubble.
  • SetBubbleText()
    • Activates the NPC's speech bubble canvas (_speechBubbleObject).
    • Displays the string given as a parameter (bubbleText) inside the speech bubble.
  • Update()
    • Call the UpdateCanvasRotation() custom function to rotate the canvas to match the camera.



3) After you have finished writing the script, add it to the Location object where the NPC will be created.

4) In the Inspector, assign the Zepeto Id, Speech Bubble Text, Speech Bubble Prefab, and Speech Bubble Y Offset.

  • Speech Bubble Text: This variable stores the dialog that the NPC character will say in the speech bubble. In our example, we store the following dialog: "Hello World".
  • Speech Bubble Prefab: This variable stores the prefab for the Speech Bubble Canvas GameObject.
  • Speech Bubble Y Offset: This variable stores the y-axis offset value for the Speech Bubble Canvas GameObject. This allows you to adjust the distance between the character and the speech bubble when you place the speech bubble above the character's head.
Example Script Setting Screen
Example Script Setting Screen


5) press the play button to execute, and you will see a speech bubble floating above the NPC's head.



Interacting with NPC



You can implement a lot of fun content by interacting with NPCs.

In this guide, we will use an example to implement a speech bubble that changes when an NPC is approached.

Document image




Setting up the Collider



1) Add a Collider component to your object to interact with the NPC and check isTrigger.

Document image




2) Resize the Collider to the extent that the player can interact with the NPC.

Document image




Creating an NPC Interaction Script



1) Create Project > Create > ZEPETO > TypeScript and rename it to NPCInteraction.

2) Write a sample script as shown below.

TypeScript




The flow of the script is as follows:

  • OnTriggerEnter(), OnTriggerExit()
    • When the trigger is detected by entering the collider area, call the SetBubbleText() custom function to set the text inside the speech bubble to changedSpeechBubbleText.
    • When it leaves the collider area, call the SetBubbleText() custom function to set the text inside the speech bubble to speechBubbleText.



3) After you have finished writing the script, add it to the Location object where the NPC will be created.

4) In the Inspector, assign the Zepeto Id, Speech Bubble Text, Speech Bubble Prefab, Speech Bubble Y Offset, and Changed Speech Bubble.

  • Speech Bubble Text: This variable stores the dialog that the NPC character will say in the speech bubble. In our example, we store the following dialog: "Hello World".
  • Speech Bubble Prefab: This variable stores the prefab for the Speech Bubble Canvas GameObject.
  • Speech Bubble Y Offset: This variable stores the y-axis offset value for the Speech Bubble Canvas GameObject. This allows you to adjust the distance between the character and the speech bubble when placing the speech bubble above the character's head.
  • Changed Speech Bubble: Stores the dialog that will be displayed in the NPC's speech bubble when the player enters the NPC's collider.
Example Script Setting Screen
Example Script Setting Screen




5) press play to execute, and when the player approaches the NPC, the text in the speech bubble will change.



Apply NPC interaction



To create a dialog format, create a UI using a panel.

The following is an example of a simple dialog composed of panels and buttons.

Document image




This is an example script that opens dialogs when interacting with NPCs and processes them when each button is pressed.

Apply this to implement interesting content.

TypeScript