簡體   English   中英

我正在嘗試制作一個在輸入觸發器后單擊按鈕時總是會發生的動畫

[英]I'm trying to make an animation that can always happen when clicking a button after entering a trigger

我正在使用 unity 2022 制作游戲,並且在輸入觸發器並按下“E”后努力讓這個動畫師[bool 變為真]。 然后在仍然按住“E”或不按它后變為假。 也在為獲取密鑰的東西而苦苦掙扎。 這不是工作伙伴,我一直在努力掙扎幾個小時,所以是的。 這是我的代碼。 它基本上是有人按下一個按鈕(我正在使用的統一對象),讓它點擊直到你再次點擊它,請幫助這是一個學校暑期項目!

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;

         }
    }
}

我主要需要新聞和代碼方面的幫助才能正常工作,所以是的:D

謝謝你

抱歉語法不好,凌晨 3 點

我想我現在明白了。

你要

  • 玩家必須在觸發器中才能開始動畫
  • 按 E 應該只觸發一次動畫

在這種情況下,我不會使用Bool參數,而是使用TriggerAnimator.SetTrigger

你的過渡應該有點像例如

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

然后你可能會做類似的事情,例如

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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM