Trying to a spawn point with a URL generated avatar so that object correctly attach to players hand. There must be a simpler way to go about this, just need a point of reference for the generated avatars hand to display items there
using System;
using ReadyPlayerMe.Core;
using UnityEngine;
namespace ReadyPlayerMe.Samples.QuickStart
{
public class ThirdPersonLoader : MonoBehaviour
{
PlayerInteraction playerInteraction;
private readonly Vector3 avatarPositionOffset = new Vector3(0, -0.08f, 0);
[SerializeField][Tooltip("RPM avatar URL or shortcode to load")]
private string avatarUrl;
private GameObject avatar;
private AvatarObjectLoader avatarObjectLoader;
[SerializeField][Tooltip("Animator to use on loaded avatar")]
private RuntimeAnimatorController animatorController;
[SerializeField][Tooltip("If true it will try to load avatar from avatarUrl on start")]
private bool loadOnStart = true;
[SerializeField][Tooltip("Preview avatar to display until avatar loads. Will be destroyed after new avatar is loaded")]
private GameObject previewAvatar;
public GameObject interactor;
public GameObject spawnPoint;
public event Action OnLoadComplete;
public GameObject RightHand;
public Transform handPoint;
// public GameObject player;
private void Start()
{
avatarObjectLoader = new AvatarObjectLoader();
avatarObjectLoader.OnCompleted += OnLoadCompleted;
avatarObjectLoader.OnFailed += OnLoadFailed;
if (previewAvatar != null)
{
SetupAvatar(previewAvatar);
}
if (loadOnStart)
{
LoadAvatar(avatarUrl);
}
playerInteraction = GetComponentInChildren<PlayerInteraction>();
}
void Update()
{
Interact();
}
public void Interact()
{
if (Input.GetButtonDown("Fire1"))
{
playerInteraction.Interact();
}
if (Input.GetButtonDown("Fire2"))
{
playerInteraction.ItemInteract();
}
if (Input.GetButtonDown("Fire3"))
{
playerInteraction.ItemKeep();
}
}
private void OnLoadFailed(object sender, FailureEventArgs args)
{
OnLoadComplete?.Invoke();
}
private void OnLoadCompleted(object sender, CompletionEventArgs args)
{
if (previewAvatar != null)
{
Destroy(previewAvatar);
previewAvatar = null;
}
SetupAvatar(args.Avatar);
OnLoadComplete?.Invoke();
// Now that the avatar is loaded, find the right hand
handPoint = args.Avatar.transform.Find("RightHand");
if (handPoint == null)
{
Debug.LogError("Right hand not found!");
}
else
{
Debug.Log("Right hand found successfully.");
}
}
private void SetupAvatar(GameObject targetAvatar)
{
if (avatar != null)
{
Destroy(avatar);
}
avatar = targetAvatar;
avatar.transform.parent = transform;
avatar.transform.localPosition = avatarPositionOffset;
avatar.transform.localRotation = Quaternion.Euler(0, 0, 0);
var controller = GetComponent<ThirdPersonController>();
if (controller != null)
{
controller.Setup(avatar, animatorController);
}
}
public void LoadAvatar(string url)
{
avatarUrl = url.Trim(' ');
avatarObjectLoader.LoadAvatar(avatarUrl);
Instantiate(interactor, this.transform, worldPositionStays:false);
Instantiate(handPoint, avatar.transform, transform.Find("RightHand"));
}
}
}