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

Animation keeps getting triggered when I step on the tiles

我正在使用一些 RPG 元素在 Unity 上开发一个 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 的新手,对它的经验为零,所以,如果你能耐心地用简单的方式解释一下,我会很高兴的!

感谢您的阅读。

编辑:这是动画师window:

编辑 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"); 
    }
}

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

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