当我踩到瓷砖时,动画不断被触发

问题描述

我正在 Unity 上开发具有一些 RPG 元素的 TopDown 2D 游戏,我这样做了,当玩家踏上放置在地图上的一组图块时,它会触发一个动画,其中包含显示一些文本的 UI。然而,当玩家一直踩在瓷砖上时,甚至当他离开触发区域时,动画会被多次调用。 我需要帮助的是如何使动画只触发一次,并且当 UI 在屏幕上时,在玩家按下 UI 中的按钮(已经编程和工作)之前,不允许玩家移动。 这是我用来制作触发器功能的代码:

public class TriggerScript : MonoBehaviour
{
    public string popUp;
    public Animator animator;

    void Start()
    {
        animator = GetComponent<Animator>();
        
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {       
            PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
            pop.PopUp(popUp);
            Debug.Log("trigger");
            animator.SetTrigger("pop");

    }
}

这里是设置文本的 PopUpSystem:

public class PopUpSystem : MonoBehaviour
{
    public GameObject popUpBox;
    public Animator popupanimation;
    public TMP_Text popUpText;
   

    public void PopUp(string text)
    {
        
        
            popUpBox.SetActive(true);
            popUpText.text = text;
            popupanimation.SetTrigger("pop");
            
        
    }
}

如果为了帮助我,您需要更多信息和细节,请在评论部分问我。

请注意,我是 Unity 的新手并且对它的经验为零,因此,如果您能耐心并以简单的方式解释事情,我会很高兴!

感谢您的阅读。

编辑:这是动画窗口:

enter image description here

编辑 2:我用于玩家移动的代码:

public class PLayerController : MonoBehaviour
{
    private Rigidbody2D MyRB;
    private Animator myAnim;
    public string popUp;

    [SerializeField]
    private float speed;
    // Start is called before the first frame update
    void Start()
    {
        MyRB = GetComponent<Rigidbody2D>();
        myAnim = GetComponent<Animator>();
    }

    private void Move()
    {
        myAnim.SetTrigger("popUp");
    }
    // Update is called once per frame
    void Update()
    {
        MyRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical")) * speed * Time.deltaTime;
        
        myAnim.SetFloat("moveX",MyRB.velocity.x);
        myAnim.SetFloat("moveY",MyRB.velocity.y);
        if ((Input.GetAxisRaw("Horizontal")==1) ||(Input.GetAxisRaw("Horizontal") == -1)||(Input.GetAxisRaw("Vertical") == 1)||(Input.GetAxisRaw("Vertical") == -1))
        {
            myAnim.SetFloat("LastMoveX",Input.GetAxisRaw("Horizontal"));
            myAnim.SetFloat("LastMoveY",Input.GetAxisRaw("Vertical"));
        }
    }
    
}

编辑 3:带有布尔值的代码:

public class TriggerScript : MonoBehaviour
{
    public string popUp;
    public Animator animator;
    bool condition = false;

    void Start()
    {
        animator = GetComponent<Animator>();
        
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (condition == false)
        {
            PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
            pop.PopUp(popUp);
            Debug.Log("trigger");
            animator.SetTrigger("pop");
            condition = true;
        }
            
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        animator.SetTrigger("close");
    }

解决方法

您所要做的就是将您的 TriggerScript 设置为如下所示:

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

public class TriggerScript : MonoBehaviour
{
    public string popUp;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
        if (collision.gameObject.tag == "Player")
        {
            pop.PopUp(popUp);
        }
            
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
        pop.closeBox();
    }
}

然后将您的 PopUpSystemScript 设置为如下所示:

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

public class PopUpSystem : MonoBehaviour
{
    public GameObject popUpBox;
    public Animator popupanimation;
    public TMP_Text popUpText;

    public void PopUp(string text)
    {                
        popUpBox.SetActive(true);
        popUpText.text = text;
        popupanimation.SetTrigger("pop");         
    }

    public void closeBox()
    {
        popupanimation.SetTrigger("close"); 
    }
}

现在单击弹出窗口并关闭动画剪辑(在动画文件夹中)并删除循环时间复选标记。您应该已经准备好看到动画的出现和消失。

要停止播放器,您可以使用 Time.timeScale。或将刚体设置为 isKenimatic。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...