Animate an object by pressing Key / or Mouse Click Unity3D

Viewed 48

currently i'm trying to animate an object by clicking or pressing a key. Unfortunately, script won't work and I have tried soooooo many other ways to do it. Here is the script:

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

public class TurnBoard : MonoBehaviour
{   
  public Animator anim;

  void Start()
  {
    anim = GetComponent<Animator>();
  }

  void OnTriggerStay(Collider player) {
    if (player.tag == "Player" && Input.GetKeyDown(KeyCode.G)){
        //Debug.Log("touchyy");
        anim.SetTrigger("turn");

    }
}}

I have assigned this script file to object itself. Object has a box collider. Animation has a trigger named "turn". When player enters to the collider zone, I want player to be able to activate animation of the object with a click/or keypress. I do get "Debug.Log" when player enters the zone. So I believe that there is no problem in detecting the collision. But just can't manage to animate object in any way.

Any help? Thank you!

1 Answers

OnTriggerStay is run like FixedUpdate (since it involves physics), so none of the Input events will work correctly inside it for the same reason that they won't work right in FixedUpdate. All Input functions must only be used in Update method.

Related