简体   繁体   English

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

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

I am working on this 3d game were a player has to press E to interact with objects.我正在开发这个 3d 游戏,玩家必须按 E 才能与对象交互。 the player has a collider that when touching a type of trigger that has this type of code, makes an object pop up showing that the player's is "selecting" something.玩家有一个对撞机,当触摸具有这种类型代码的触发器时,会弹出一个对象,显示玩家正在“选择”某物。 when the player is in the trigger I made it were when they press E, the objects animation plays.当玩家在触发器中时,我做到了,当他们按下 E 时,对象动画就会播放。 When adding the objecting selecting thing it made it now that I cant make the animation play when pressing 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);
        }
    }
}

OnTrigger methods are called when another object with a Collider enters in your object Collider.当另一个带有 Collider 的对象进入您的对象 Collider 时,将调用 OnTrigger 方法。
First, make sure everything was well set up well, and maybe you want to use OnCollision instead of OnTrigger methods, if you want to detect a collision whether than a "penetration" if i may say首先,确保一切都设置得很好,也许你想使用 OnCollision 而不是 OnTrigger 方法,如果你想检测碰撞而不是“穿透”,如果我可以说

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

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