- Home /
Retrigger animation after play through
Hi! I'm new to Unity and building a VR model for a future transportation vehicle as a design demo. Currently I'm using the code below to animate different objects such as the door and trunk with an on click trigger. The code as of now allows me to begin the animation. But once it finishes I can't reclick my trigger to repeat the animation. How can I reset my anim.enable to false at the end of the animation? Thanks in advance -- I've been digging through forums for a bit and seem stuck.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Valve.VR.InteractionSystem {
[RequireComponent( typeof( Interactable ) )]
public class PressToStartAnimation : MonoBehaviour {
public Animator anim;
private int lastPressed = 0;
void Start () {
anim.enabled = false;
}
void OnHandHoverBegin( Hand hand ) {
this.GetComponent<Renderer> ().material.SetColor("_Color", Color.green);
}
void HandHoverUpdate( Hand hand ) {
//Trigger got pressed
if ( hand.GetStandardInteractionButton() )
{
this.GetComponent<Renderer> ().material.SetColor("_Color", Color.red);
anim.enabled = true;
StartCoroutine(WaitForAnimationToPlay());
}
}
IEnumerator WaitForAnimationToPlay()
{
yield return new WaitForSeconds(5);
anim.enabled = false;
lastPressed = lastPressed + 1;
}
void update () {
if (lastPressed == 2) {
anim.enabled = false;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Animated Object Problem 1 Answer
Presure plate that triggers an animation 1 Answer
call/trigger animation of other object 2 Answers
object animation trigger 0 Answers
Trigger sound on another gameobject 1 Answer