Question by
unity_22CE126B17A786DF3166 · Jan 26 at 12:12 AM ·
vr
How to stop a loop in hand animation vr?
Hi, I am trying to create a vr animated hand that can close and open by pressing button on the controller. The problem is that i have already unchecked the Loop Time box in the animation but it still loop when i press. I am using the Oculus quest 1.
This is my code Hand.cs code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class Hand : MonoBehaviour
{
Animator animator;
private float triggerTarget;
private float gripTarget;
private float gripCurrent;
private float triggerCurrent;
public float speed;
private string animatorGripParam = "Grip";
private string animatorTriggerParam = "Trigger";
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
AnimateHand();
}
internal void SetGrip(float v)
{
gripTarget = v;
}
internal void SetTrigger(float v)
{
triggerTarget = v;
}
void AnimateHand()
{
if (gripCurrent != gripTarget)
{
gripCurrent = Mathf.MoveTowards(gripCurrent, gripTarget, Time.deltaTime * speed);
animator.SetFloat(animatorGripParam, gripCurrent);
}
if (triggerCurrent != triggerTarget)
{
triggerCurrent = Mathf.MoveTowards(triggerCurrent, triggerTarget, Time.deltaTime * speed);
animator.SetFloat(animatorTriggerParam, triggerCurrent);
}
}
}
This is my HandController.cs code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
[RequireComponent(typeof(ActionBasedController))]
public class HandController : MonoBehaviour
{
ActionBasedController controller;
public Hand hand;
void Start()
{
controller = GetComponent<ActionBasedController>();
}
// Update is called once per frame
void Update()
{
hand.SetGrip(controller.selectAction.action.ReadValue<float>());
hand.SetTrigger(controller.activateAction.action.ReadValue<float>());
}
}
capture.png
(12.8 kB)
Comment
Your answer
Follow this Question
Related Questions
Xcode clang error 1 Answer
android camera rotation using gyro sensor 1 Answer
VR - POUR WATER INTO A FLASK/GLASS/BEAKER 1 Answer
Generic UI interaction for VR 0 Answers
How can I track the turn on of HTC VIVE controllers in Stem VR? 0 Answers