简体   繁体   English

我正在努力使玩游戏的人可以在触发器中按他们想要的次数按“E” | Unity 3d + 动画师

[英]I'm trying to make it so the person playing the game can press "E" as much as they want in the trigger | Unity 3d + animator

I am working on this Unity 3D game I have been making were you, the player can click random buttons(Don't ask why).我正在开发这个我一直在制作的 Unity 3D 游戏,玩家可以点击随机按钮(不要问为什么)。 In this game I made it were the player can enter a trigger and press E to make the animation trigger only once while clicking it, which perfectly went well.在这个游戏中,我做了一个玩家可以输入一个触发器,然后按下E来触发一次动画,同时点击它,效果非常好。 but the only problem is that the "Player" can only make the animation trigger once and than have to leave the trigger and re-enter it to make the animation trigger and work again when pressing E .但唯一的问题是“播放器”只能使动画触发一次,而必须离开触发器并重新进入它才能使动画触发并在按下E时再次工作。

My Goal is to have the animation always work when clicking, not holding E everytime when only in the trigger.我的目标是让动画在单击时始终有效,而不是仅在触发器中每次都按住E。

this is the code for the button that I have made.这是我制作的按钮的代码。

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

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

Instead of starting a coroutine inside onTriggerEnter, just have a bool to be set as true when the player enters the trigger and retrieve the input inside the Update with that bool as a condition.无需在 onTriggerEnter 中启动协程,只需在玩家进入触发器时将 bool 设置为 true,并使用该 bool 作为条件检索 Update 中的输入。 PS: you can also add a cooldown timer so that the player cannot spam E inside the trigger. PS:您还可以添加一个冷却计时器,以便玩家无法在触发器内发送E。 Example:例子:

bool canAnimate;

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

    canAnimate = true;
}

private void Update()
{
if (Input.GetKeyDown(KeyCode.E) && canAnimate)
 {
         Tributton.SetTrigger("Fiveyon");

 }
}

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);
 canAnimate = false;
}

PS: I am sure that there would be a better way to achieve this. PS:我相信会有更好的方法来实现这一点。 Let me know if it helps :)让我知道它是否有帮助:)

You need你需要

OnTriggerStay()

You can refer Unity Docs for more details.您可以参考Unity 文档了解更多详细信息。

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

相关问题 触发器没有响应我在Unity 3D中的需求 - Trigger's not responding how I want in unity 3d Unity 3d 第一人称游戏开发表现怪异; 只能上下旋转 - Unity 3d first person game development acting weird; only can rotate up and down 我怎样才能让这个脚本不消耗这么多? 统一 - How can I make this script not consume so much? Unity 尝试为我的自上而下的统一 3D/2.5D 游戏制作 Dash - Trying to make a Dash for my top down unity 3D/2.5D Game 按下 E | 时不播放动画团结 3d - Animation not playing when pressing E | unity 3d 我正在尝试这样做,触发时,玩家停止移动 - I'm trying to make as so, on trigger, the player stops moving 我正在尝试在 Unity 中执行传送脚本 3d 我不是专家 - I'm trying to do a teleport script in Unity 3d I'm not an expert 我正在尝试让我的 2D 统一角色跳跃 - I'm trying to make my 2D unity character jump Unity 3D 试图引用动画师中附加到空状态的脚本。 获取组件不工作 - Unity 3D Trying to reference script that is attached to empty state in the animator. GetComponent Not Working “错误 CS0103:当前上下文中不存在名称‘targetAngle’”在统一 3d 中尝试制作第三人称相机时 - "error CS0103: The name 'targetAngle' does not exist in the current context" in unity 3d while was trying to make a 3rd person camera
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM