繁体   English   中英

按下 E | 时不播放动画团结 3d

[英]Animation not playing when pressing E | unity 3d

我正在开发这个 3d 游戏,玩家必须按 E 才能与对象交互。 玩家有一个对撞机,当触摸具有这种类型代码的触发器时,会弹出一个对象,显示玩家正在“选择”某物。 当玩家在触发器中时,我做到了,当他们按下 E 时,对象动画就会播放。 添加对象选择的东西时,现在我无法在按 E 时播放动画

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

public class Fixedpress : MonoBehaviour
{
    public Animator Tributton;
    public GameObject GM;
    private Coroutine routine;

    private void Start()
    {
        GM.SetActive(false);        
    }



    private void OnTriggerStay(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("LookTrig")) {

            GM.SetActive(true);
            
            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("LookTrig")) {
            GM.SetActive(false);
            return; 
            
        }

        // when exiting the trigger stop the routine so later button press is not handled
        if (routine != null)
        {
            
            StopCoroutine(routine);
            GM.SetActive(false);
        }
    }
}

当另一个带有 Collider 的对象进入您的对象 Collider 时,将调用 OnTrigger 方法。
首先,确保一切都设置得很好,也许你想使用 OnCollision 而不是 OnTrigger 方法,如果你想检测碰撞而不是“穿透”,如果我可以说

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM