- Home /
Question by
unity_7R2PuZWvsHXghQ · Jun 06, 2021 at 05:08 PM ·
animationprefabvr
Why does my prefab instantiate its model twice?
I have followed a tutorial for VR hand animations. When I started the game I noticed that the animations were working, but the initial hand was in the same position and duplicated:
After looking at my objects I noticed that the model was instantiated twice (one instance is animated and the other isn't). Why does this happen?
This is the script that instantiates the VR hands:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class HandPresence : MonoBehaviour
{
public InputDeviceCharacteristics controllerCharacteristics;
public GameObject handModelPrefab;
private InputDevice targetDevice;
private GameObject spawnedHandModel;
private Animator handAnimator;
void Start()
{
TryInitialize();
}
void TryInitialize()
{
List<InputDevice> devices = new List<InputDevice>();
InputDevices.GetDevicesWithCharacteristics(controllerCharacteristics, devices);
foreach (var item in devices)
{
Debug.Log(item.name + item.characteristics);
}
if (devices.Count > 0)
{
targetDevice = devices[0];
spawnedHandModel = Instantiate(handModelPrefab, transform);
handAnimator = spawnedHandModel.GetComponent<Animator>();
}
}
void UpdateHandAnimation()
{
if(targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue))
{
handAnimator.SetFloat("Trigger", triggerValue);
}
else
{
handAnimator.SetFloat("Trigger", 0);
}
if (targetDevice.TryGetFeatureValue(CommonUsages.grip, out float gripValue))
{
handAnimator.SetFloat("Grip", gripValue);
}
else
{
handAnimator.SetFloat("Grip", 0);
}
}
void Update()
{
if(!targetDevice.isValid)
{
TryInitialize();
} else
{
spawnedHandModel.SetActive(true);
UpdateHandAnimation();
}
}
}
unknown.png
(11.8 kB)
annotation-2021-06-06-200519.png
(12.3 kB)
Comment
Answer by unity_7R2PuZWvsHXghQ · Jun 06, 2021 at 10:05 PM
I realised that instead of adding a prefab to the Hand Controllers, I was actually adding objects from the scene. Creating a prefab from those objects and then adding those to the Hand controllers fixed it for me.