微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决当我踩到瓷砖时,动画不断被触发

我正在 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。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。