Home > other >  I'm trying to make an animation that can always happen when clicking a button after entering a
I'm trying to make an animation that can always happen when clicking a button after entering a

Time:07-21

I'm making a game with unity 2022 and was struggling to make this animator [bool turn true] after entering a trigger and pressing "E". and then go false after still holding "E" still or not pressing it. also am struggling with Get Key stuff. it isn't working mate and I have been trying for hours at a time with struggles so yeah. this is my code. it's basically someone pressing a button (the object in unity I'm using), making it click until you click it again, please help this is for a school summer project!

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

class Fixedpress : MonoBehaviour
{
    public Animator Tributton;
    bool YUIp = false;
    // Start is called before the first frame update

    void OnTriggerEnter(Collider other)
    {
       if(other.tag == "player")
        {

            YUIp = true;

        }
    }
    void OnTriggerStay(Collider other)
    {
        
        if (other.tag == "Player" && YUIp == true)
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                Tributton.SetBool("Fiveyon", true);
                Debug.Log("THY");
            }
            else if (Input.GetKey(KeyCode.E))
            {
                Tributton.SetBool("Fiveyon", false);
                Debug.Log("THe");
            }
            else if (Input.GetKeyUp(KeyCode.Return))
            {

                Tributton.SetBool("Fiveyon", false);
                Debug.Log("THane");
            }
        }
        
    }
    void OnTriggerExit(Collider other)
    {
         if(other.tag == "Player")
         {

            YUIp = false;

         }
    }
}

I mostly need help with the press e code stuff to make work so yeah :D

thank you

sorry for the bad grammar its 3 am rn

CodePudding user response:

I think I got it now.

You want

  • Player has to be in the trigger in order to start the animation
  • Pressing E should trigger the animation only ONCE

I would no use a Bool parameter in that case but rather a Trigger and then Animator.SetTrigger.

Your transitions should be somewhat like e.g.

Idle State --condition-Fiveyon--> Your Animation
Your Animation --exit-time-no-further-condition--> Idle State

Then you could probably do something like e.g.

public class Fixedpress : MonoBehaviour
{
    public Animator Tributton;

    private Coroutine routine;

    private void OnTriggerEnter(Collider other)
    {
        // in general rather use CompareTag instead of ==
        // it is slightly faster and also shows an error if the tag doesn't exist instead of failing silent
        if(!other.CompareTag("player")) return;
        
        // just in case to prevent concurrent routines
        if(routine != null) StopCoroutine(routine);

        // start a new Coroutine
        routine = StartCoroutine(WaitForKeyPress());
    }

    private IEnumerator WaitForKeyPress()
    {
        // check each FRAME if the key goes down
        // This is way more reliable as OnTriggerStay which is called
        // in the physics loop and might skip some frames
        // This also prevents from holding E while entering the trigger, it needs to go newly down 
        yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.E));
           
        // set the trigger once and finish the routine
        // There is no way to trigger twice except exit the trigger and enter again now
        Tributton.SetTrigger("Fiveyon");
        Debug.Log("Fiveyon!");

        // If you even want to prevent this from getting triggered ever again simply add
        enabled = false;
        // Now this can only be triggered ONCE for the entire lifecycle of this component 
        // (except you enable it from the outside again of course)
    }

    void OnTriggerExit(Collider other)
    {
         if(!other.CompareTag("player")) return;

         // when exiting the trigger stop the routine so later button press is not handled
         if(routine != null) StopCoroutine(routine);
    }
}
  • Related