Skip to main content

NPC Avatar

The next section is going to spawn an NPC Avatar using a locally-stored Avatar definition. This will use the Using Multiple Avatars sample scene to create a locally stored Avatar definition.

Open the Sample Scene

In the Project window, open the Assets > Samples > Genies Avatar SDK > X.Y.Z > Sample Starter Scenes > Using Multiple Avatars > Scenes folder. Double click the MultipleAvatars scene to open it.

Open Sample

Play the Scene

Once the scene is opened, press the Play button. Once you are logged in, make sure the default values for the UIs are set to:

  • Save Locally & Exit Editor
  • Test-Profile

Then click one of the Avatars to open the Avatar Editor.

Play Sample

info

These two UIs are setting the Avatar Editor to save a locally stored Avatar definition under the name Test-Profile once the user clicks the Save button.

Save a Definition

Use the Avatar Editor to create the look of the NPC. Once finished, click the Save button to locally store the definition you created. Exit Play mode.

Save Definition

Find the Test Profile Definition

In the Project window, open the Assets > Genies > AvatarEditor > Profiles > Resources folder. Make sure the Test-Profile definition is successfully created.

Test Profile

Create NPC Spawn Object

Open the SampleScene in the Assets > Scenes folder. Create a new empty Game Object named NpcSpawn.

NPC Spawn

Create NPC Controller Script

In the Hierarchy window, select the NpcSpawn object. In the Inspector window, click the Add Component button near the bottom and then select the bottom option to New Script. Name the script NpcController.

Then set the Position property to Z: 3 and Rotation property to Y: 180.

NPC Controller

Add Code to the Script

Open the NpcController script and add this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Genies.Sdk;

public class NpcController : MonoBehaviour
{
void Start()
{
if (!AvatarSdk.IsLoggedIn)
{
AvatarSdk.Events.UserLoggedIn += LoadNpc;
return;
}

LoadNpc();
}

private void OnDestroy()
{
AvatarSdk.Events.UserLoggedIn -= LoadNpc;
}

private async void LoadNpc()
{
var npcAvatar = await AvatarSdk.LoadFromLocalAvatarDefinitionAsync("Test-Profile");
npcAvatar.Root.transform.parent = this.transform;
npcAvatar.Root.transform.position = this.transform.position;
npcAvatar.Root.transform.rotation = this.transform.rotation;
}
}
info

The AvatarSdk.LoadFromLocalAvatarDefinitionAsync("Test-Profile"); method is loading the locally stored Avatar definition that you created earlier.

Test the Code

Save the code and enter Play mode. The NPC Avatar should load in front of the player Avatar.

Test NPC