我试图让玩家在触发时停止移动

问题描述

我正在 Unity 中开发 TopDown 2D 游戏。 这个想法是,当玩家踩到某个图块时,会弹出一个带有文本的 UI(已经工作)并且玩家停止移动,直到玩家点击按钮(已经编程并工作)并且 UI 消失。 我被建议将 RigidBody2D 转为运动学,但它不起作用,它只是做它以前做的事情。 难道我做错了什么? 以下是磁贴上触发脚本的代码:

public class TriggerScript : MonoBehaviour
{
    public string popUp;
    public void Start()
    {
 
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        Rigidbody2D Player = GameObject.Find("Player").GetComponent<Rigidbody2D>();
        PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
        if (collision.gameObject.tag == "Player")
        {
            pop.PopUp(popUp);
            Player.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
        }

    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        Rigidbody2D Player = GameObject.Find("Player").GetComponent<Rigidbody2D>();
        PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
        pop.closeBox();
        Player.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
    }
}

解决方法

弹出系统.cs

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 PLayerController mb;
    public void PopUp(string text)
    {                
        popUpBox.SetActive(true);
        popUpText.text = text;
        popupanimation.SetTrigger("pop");         
    }

    public void closeBox()
    {
        popupanimation.SetTrigger("close");
        mb.camMove = true;
    }
}

播放器控制器.cs

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

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

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

    // Update is called once per frame
    void Update()
    {
        if (camMove)
        {
            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"));
            }
        }

        if (!camMove)
        {
            MyRB.velocity = new Vector2(0,0);
        }

    }
    
}

TriggerScript.cs

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

public class TriggerScript : MonoBehaviour
{
    public string popUp;
    Animator popAnim;
    public PLayerController mb;

    private void Start()
    {
        popAnim = GameObject.FindGameObjectWithTag("PopUpBox").GetComponent<Animator>();
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
        var myp = GameObject.FindGameObjectWithTag("Player").GetComponent<PLayerController>();
        
        if (collision.gameObject.tag == "Player")
        {
            pop.PopUp(popUp);
            mb.camMove = false;
        }

        if (popAnim.GetCurrentAnimatorStateInfo(1).IsName("close"))
        {
            mb.camMove = true;
            Debug.Log("close");
        }


    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
        pop.closeBox();
    }
}

注意事项:

1- 创建一个新标签并将其命名为 PopUpBox。然后将此标签分配给触发器游戏对象。

2- 给 PopUpBox GameObject 的按钮(被禁用的那个)分配 GameManager GameObject 并在它的函数中选择 PopUpSystem.closeBox

3- 在 Trigger GameObject 和 GameManager 中,在 Mb 字段中分配 Player。

希望对你有帮助。您可以更多地使用它以获得更好的结果。

相关问答

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