繁体   English   中英

Unity2d播放器对撞机进入触发按键不起作用

[英]Unity2d player collider entering trigger pressing key does not work

我正在做一个学校项目,我需要有关 trigger2dstay 的帮助。 我试图让它成为当我的玩家的对撞机标签进入触发器对撞机时,会弹出一个有效的图像,但我也试图让它在播放做同样的事情并按下E时,它会触发一个动画,但是当我的 2d 玩家走进触发器并按下E时,什么也没有发生。 E仅在您移动并按下它时才有效,而不是静止不动。

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

public class buttonele : MonoBehaviour
{
    
    public GameObject Obje;
    public GameObject blockers;
    public GameObject eledoorn;
    public GameObject eledormation;
    bool Unlock;
    // Start is called before the first frame update
    void Start()
    {
        Obje.SetActive(false);
        eledormation.SetActive(true);
        Unlock = false;
    }

    void OnTriggerStay2D(Collider2D other)
    {
        if (other.tag == "Player")
        {
            Unlock = true;

            Obje.SetActive(true);
        }

        if (Unlock == true && Input.GetKeyDown(KeyCode.E))
        {
            Destroy(blockers);
            Destroy(eledoorn);

            eledormation.GetComponent<Animator>().Play("eleopen");
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        if (other.tag == "Player")
        {
            Obje.SetActive(false);
        }
    }
}

下面的代码意味着你的玩家进入了另一个玩家的对撞机,你真的想要吗?

if (other.tag == "Player")

要解决这个问题,请测试代码的逻辑。 如果您希望您的玩家能够通过进入某个区域来使用E键,您需要在此字段中输入该地点的标签。 如果您想打开这些盒子,请给它们一个特殊的标签(例如Item )并按如下方式更改代码。

if (other.CompareTag("Item"))
{
    // do something..
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM