Search code examples
c#unity-game-enginevirtual-reality

Animations are Not Playing When Entering In Trigger?


i am creating a vr project i have a simple cube in a scene with a box collider on it with trigger checked on it now what i want is that when my vr hand enters the trigger of that cube the grab animation of my vr hand should play but when i enter my hand into the trigger of cube nothing happens then for testing purpose i debug.log an message that wheather its detecting or not that is fine it is detecting when my hand is entered and when it leaves but animation is not playing here is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VRHandsController : MonoBehaviour {

public VRTK.VRTK_ControllerEvents vrcontroller;
public VRTK.VRTK_InteractGrab vrtkgrab;
Animator anim;
// Start is called before the first frame update
void Start()
{
    anim = GetComponent<Animator>();
    //   
        anim.SetBool("indicate", false);
    anim.SetBool("grab", false);
    anim.SetBool("idle", true);
    //anim.SetBool("isGrabbing", false);
}

// Update is called once per frame
void Update()
{
    if (vrcontroller.touchpadPressed)
    {

        anim.SetBool("indicate", true);
        anim.SetBool("grab", false);
        anim.SetBool("idle", false);
        //anim.SetBool("isGrabbing", true);
    }
    else if (vrtkgrab.IsGrabButtonPressed())
    {

        anim.SetBool("indicate", false);
        anim.SetBool("grab", true);
        anim.SetBool("idle", false);
    }

    else if (vrcontroller.triggerClicked)
    {
        anim.SetBool("indicate", true);
        anim.SetBool("grab", false);
        anim.SetBool("idle", false);
    }
    else
    {

        anim.SetBool("indicate", false);
        anim.SetBool("grab", false);
        anim.SetBool("idle", true);
        //a///nim.SetBool("isGrabbing", false);
    }
}

private void OnTriggerEnter(Collider other)
{
    if(other.gameObject.tag == "object")
    {
        anim.SetBool("indicate", false);
        anim.SetBool("grab", true);
        anim.SetBool("idle", false);
        Debug.Log("colliding>>>>>");
    }
}
private void OnTriggerStay(Collider other)
{
    if (other.gameObject.tag == "object")
    {
        anim.SetBool("indicate", false);
        anim.SetBool("grab", true);
        anim.SetBool("idle", false);
        Debug.Log("colliding>>>>>");
    }
}

private void OnTriggerExit(Collider other)
{
    if (other.gameObject.tag == "object")
    {
        anim.SetBool("indicate", false);
        anim.SetBool("grab", false);
        anim.SetBool("idle", true);
        Debug.Log("leaving trigger");
    }
}

}


Solution

  • You are setting grab to false, in the else part:

    else if (vrcontroller.triggerClicked)
    ...
    } else {
    anim.SetBool("grab", false);
    ...
    

    in every Update() call!

    So whenever you don't press the trigger, the grab is set to false. Entering the trigger will set it to true, only to be resetted in the same frame. (Collisions run before Update)

    • You should not set it to false like that. You should set it to false if vrcontroller.triggerReleased (if there is such a thing) If there is no "triggerReleased" you need a helper-boolean.