Unity3d 团结只在一个目标上工作

Unity3d 团结只在一个目标上工作,unity3d,unity5,Unity3d,Unity5,我有一个问题,当我的场景中有多个门时,只有当我靠近其中一个门时才会出现文本。我真的不知道问题出在哪里,因为Unity不会抛出任何异常。这是我的密码: using UnityEngine; using System.Collections; using UnityEngine.UI; public class Door : MonoBehaviour { public GameObject doorBody; Transform player; private Audio

我有一个问题,当我的场景中有多个门时,只有当我靠近其中一个门时才会出现文本。我真的不知道问题出在哪里,因为Unity不会抛出任何异常。这是我的密码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Door : MonoBehaviour {
    public GameObject doorBody;
    Transform player;
    private AudioSource doorSound;
    public float doorOpenAngle = 90.0f;
    public float doorCloseAngle = 0.0f;
    public float doorAnimSpeed = 2.0f;
    private Quaternion doorOpen = Quaternion.identity;
    private Quaternion doorClose = Quaternion.identity;
    private Transform playerTrans = null;
    public bool doorStatus = false; //false = close, true = open
    private bool doorGo = false;
    public Text text;

    void Start()
    {
        doorStatus = false; //door is open
        doorSound = GetComponent<AudioSource>();
        doorOpen = Quaternion.Euler(0, doorOpenAngle, 0);
        doorClose = Quaternion.Euler(0, doorCloseAngle, 0);
        playerTrans = GameObject.FindWithTag("Player").transform;
        text.text = "";
    }
    void Update()
    {
            if (Vector3.Distance(playerTrans.position, this.transform.position) < 2f)
            {
            text.text = "Press 'E' to use";
            if (Input.GetKeyDown(KeyCode.E) && !doorGo)
                {
                    doorBody.GetComponent<BoxCollider>().enabled = false;
                    doorSound.mute = false;
                    if (doorStatus)
                    { //close door
                        StartCoroutine(this.moveDoor(doorClose));
                        doorSound.Play();
                    }
                    else { //open door
                        StartCoroutine(this.moveDoor(doorOpen));
                        doorSound.Play();
                    }
                }
            }
            else
            {
            text.text = "";
        }
    }

    public IEnumerator moveDoor(Quaternion dest)
    {
        doorGo = true;
        while (Quaternion.Angle(transform.localRotation, dest) > 4.0f)
        {
            transform.localRotation = Quaternion.Slerp(transform.localRotation, dest, Time.deltaTime * doorAnimSpeed);
            yield return null;
        }
        //Change door status
        doorBody.GetComponent<BoxCollider>().enabled = true;
        doorStatus = !doorStatus;
        doorGo = false;
        yield return null;
    }
}
使用UnityEngine;
使用系统集合;
使用UnityEngine.UI;
公共类门:单一行为{
公共游戏对象门体;
转换玩家;
私人声源;
公共浮门开启角度=90.0f;
公共浮门关闭角度=0.0f;
公共浮动门速度=2.0f;
私有四元数doorOpen=Quaternion.identity;
私有四元数门关闭=四元数.identity;
私有转换playerTrans=null;
public bool doorStatus=false;//false=close,true=open
私人布尔门戈=假;
公共文本;
void Start()
{
doorStatus=false;//门已打开
doorSound=GetComponent();
doorOpen=Quaternion.Euler(0,doorOpenAngle,0);
doorClose=四元数.Euler(0,doorCloseAngle,0);
playerTrans=GameObject.FindWithTag(“玩家”).transform;
text.text=”“;
}
无效更新()
{
if(向量3.距离(playerTrans.position,this.transform.position)<2f)
{
text.text=“按“E”使用”;
if(Input.GetKeyDown(KeyCode.E)和&!doorGo)
{
doorBody.GetComponent().enabled=false;
doorSound.mute=false;
如果(门状态)
{//关门
启动例行程序(此。移动门(门关闭));
门的声音;
}
否则{//开门
启动例行程序(此。移动门(门打开));
门的声音;
}
}
}
其他的
{
text.text=”“;
}
}
公共IEnumerator移动门(四元数目标)
{
doorGo=true;
while(四元数角度(transform.localRotation,dest)>4.0f)
{
transform.localRotation=Quaternion.Slerp(transform.localRotation,dest,Time.deltaTime*速度);
收益返回空;
}
//更改门状态
doorBody.GetComponent().enabled=true;
doorStatus=!doorStatus;
doorGo=false;
收益返回空;
}
}

坦克你。

我认为问题是你只把脚本附加到一个门项目上。检查您是否已将脚本附加到两个门

如果问题不在于此,那么可能导致问题的另一个方面是

            if (Vector3.Distance(playerTrans.position, this.transform.position) < 2f)
            {
            text.text = "Press 'E' to use";
            if (Input.GetKeyDown(KeyCode.E) && !doorGo)
                {
                    doorBody.GetComponent<BoxCollider>().enabled = false;
                    doorSound.mute = false;
                    if (doorStatus)
                    { //close door
                        StartCoroutine(this.moveDoor(doorClose));
                        doorSound.Play();
                    }
                    else { //open door
                        StartCoroutine(this.moveDoor(doorOpen));
                        doorSound.Play();
                    }
                }
            }
            else
            {
            text.text = "";
if(矢量3.距离(playerTrans.position,this.transform.position)<2f)
{
text.text=“按“E”使用”;
if(Input.GetKeyDown(KeyCode.E)和&!doorGo)
{
doorBody.GetComponent().enabled=false;
doorSound.mute=false;
如果(门状态)
{//关门
启动例行程序(此。移动门(门关闭));
门的声音;
}
否则{//开门
启动例行程序(此。移动门(门打开));
门的声音;
}
}
}
其他的
{
text.text=”“;
else部分可能会导致文本在您不希望的情况下设置为NULL。例如,如果您转到两扇门中的一扇门,该扇门的if语句将为true,文本将显示,然后当您离开第一扇门时,代码将转到else并将文本设置为NULL。然后,当您转到第二扇门时,new door将尝试将文本设置为正确的字符串,但第一个door代码将覆盖它并将文本设置为NULL


如果这就是问题所在,那么最好的解决方案很可能是将门接近检测代码切换到播放机。这样,播放机本身就只需检查是否有任何门在范围内,而不是每个门不断检查每次更新以查看播放机是否在范围内。

脚本是附加到预制对象还是每个i单个门?是的,它是…………您的脚本似乎没有问题,如果它适用于预置的一个实例,那么它必须适用于所有实例,除非您在放置后对其中一个实例进行了更改。因此,如果不查看预置结构和两个实例之间的比较,将很难确定问题所在m、 …只是一个“旁注”:你真的需要将场景中的每个门之间的距离与每一帧的玩家位置进行比较?为什么不使用简单的触发器?Jessy final concern与我上面关于“粗糙”的评论相匹配显示文本的方式……你真的应该了解触发器,有很多关于通过互联网使用触发器开门的教程……是的,有比计算每帧门和播放器之间的距离更好的检查方法。我知道其他方法,但我写了这个脚本只用于测试,我将对它进行大量编辑:)不过非常感谢您的回答!关键字
触发器