简体   繁体   English

我正在尝试这样做,触发时,玩家停止移动

[英]I'm trying to make as so, on trigger, the player stops moving

I'm developing a TopDown 2D game in Unity.我正在 Unity 中开发 TopDown 2D 游戏。 The idea is, when the player steps on a certain tile, a UI with text pops up (already working) and the player stops moving until the player clicks a button (already programmed and working) and the UI disappears.这个想法是,当玩家踩到某个图块时,会弹出一个带有文本的 UI(已经工作)并且玩家停止移动,直到玩家单击一个按钮(已经编程并工作)并且 UI 消失。 I was advised to turn the RigidBody2D to kinematic however it doesn't work and it just does what it used to do.有人建议我将 RigidBody2D 转为 kinematic,但它不起作用,它只是做它过去做的事情。 Am I doing something wrong?难道我做错了什么? Here is the code for the trigger script on the tiles:以下是图块上触发脚本的代码:

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

PopUpSystem.cs弹出系统.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;
    }
}

PLayerController.cs播放器控制器.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触发器脚本.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();
    }
}

Things to keep in mind:要记住的事情:

1- Make a new tag and call it PopUpBox. 1- 制作一个新标签并将其命名为 PopUpBox。 then assign this tag to the Trigger GameObject.然后将此标签分配给 Trigger GameObject。

2- To the button of the PopUpBox GameObject (the one which is disabled) assign GameManager GameObject and in its function select PopUpSystem.closeBox 2- 到 PopUpBox GameObject 的按钮(被禁用的那个)分配 GameManager GameObject 并在其 function select PopUpSystem.closeBox

3- In both Trigger GameObject and GameManager assign Player in Mb field. 3- 在 Trigger GameObject 和 GameManager 中,在 Mb 字段中分配 Player。

Hope this help you.希望这对您有所帮助。 You can play with that more to get better results.您可以多玩一些以获得更好的结果。

暂无
暂无

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

相关问题 我正在努力使玩游戏的人可以在触发器中按他们想要的次数按“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'm trying to make it so that the number cant be increased in a TextBox 我如何使它在 UI 打开时我的角色/相机停止移动? - How do i make it so my character/camera stops moving when UI is open? 我正在尝试制作音乐播放器,但我无法弄清楚 openfiledialog 多选是如何工作的 - I'm trying to make a music player, but I can't figure out how does the openfiledialog multiselect work 我正在尝试制作一个在输入触发器后单击按钮时总是会发生的动画 - I'm trying to make an animation that can always happen when clicking a button after entering a trigger 玩家在与GameObject接触时停止移动 - Player stops moving upon contact with a gameObject 不移动鼠标时如何使播放器闲置? - How can i make the player idle when not moving the mouse? 如何让玩家在碰到对撞机时停止移动? - How can i make a player stop moving when it touchs a collider? 我正在尝试为我的游戏制作一个随机数生成器并为其分配一个整数,以便我以后可以使用它但给我一个错误 - I'm trying to make a random number generator for my game and assign a integer to it so i can use it later but give me a error 我该如何做,以便播放器在重播时重新激活? - How do I make it so the player reactivates on replay?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM