简体   繁体   English

Unity 3D门脚本

[英]Unity 3D Door Script

I have written a door script for big vault doors. 我已经为大型金库门编写了门脚本。 My problem is the following: if I assign the script to more than one door in the scene, all doors open and close together at the same time that one of them is touched. 我的问题如下:如果将脚本分配给场景中的多扇门,则在触摸其中一扇的同时,所有门同时打开和关闭。

using UnityEngine;
using System.Collections;

public class BigDoorScript : MonoBehaviour
{

    private bool doorOpen = false;
    private Ray ray;
    private RaycastHit hit;
    private float distance = 5.0f;
    public GameObject door;

    private void Update()
    {
        if (Input.GetKeyDown("e"))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, distance))
            {
                if (!doorOpen)
                {
                    door.transform.Translate(new Vector3(0.0f, 0.0f, 4.0f));
                    doorOpen = true;
                }
                else
                {
                    door.transform.Translate(new Vector3(0.0f, 0.0f, -4.0f));
                    doorOpen = false;
                }
            }
        }
    }
}

You don't have to declare the door gameobject at all. 您根本不必声明门游戏对象。 And you can play with the hitinfo ("hit"). 您可以使用hitinfo(“ hit”)。 Try this - 尝试这个 -

using UnityEngine;
using System.Collections;

public class BigDoorScript : MonoBehaviour
{

    private bool doorOpen = false;
    private Ray ray;
    private RaycastHit hit;
    private float distance = 5.0f;

    private void Update()
    {
        if (Input.GetKeyDown("e"))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, distance))
            {
                if(hit.collider.gameObject.name == "door"){//Check that your ray is colliding with the door
                    if (!doorOpen)
                    {
                        hit.transform.Translate(new Vector3(0.0f, 0.0f, 4.0f));
                        doorOpen = true;
                    }
                    else
                    {
                        hit.transform.Translate(new Vector3(0.0f, 0.0f, -4.0f));
                        doorOpen = false;
                    }
                }
            }
        }
    }
}

But make sure that the name of your door gameobject is "door" without quotes. 但是请确保您的门游戏对象的名称是“门”,不带引号。

All of your doors open because you're not actually checking whether or not the hit object from your Raycast is actually the door object your script is holding a reference to. 您所有的门都打开了,因为您实际上并未检查Raycast中的命中对象是否实际上是脚本持有引用的门对象。 Imtiaj touches on this briefly, but string compares on Game Objects are a bad idea and does not limit yourself to one object as many Game Objects an have the same name. Imtiaj简要地谈到了这一点,但是在Game Objects上进行字符串比较不是一个好主意,并且不会将自己局限于一个对象,因为许多Game Objects具有相同的名称。

Therefore, any collision will open all of the doors with this script. 因此,任何冲突都将使用此脚本打开所有的门。

You want to use 你想用

if (hit.collider.gameObject == door)

I think problem is your distance value is very big and when you press a door other door in that distance take that hit as well. 我认为问题在于您的距离值非常大,并且当您按该距离按另一扇门时,也会受到打击。 try reducing that value 尝试降低价值

I don't think you need to declare a door GameObject. 我认为您无需声明门GameObject。 Just use the gameObject property on the monobehavior to move the door the script is attached to: 只需使用gameObject上的gameObject属性来移动脚本附加到的门:

using UnityEngine;
using System.Collections;

public class BigDoorScript : MonoBehaviour
{
    private bool doorOpen = false;
    private Ray ray;
    private RaycastHit hit;
    private float distance = 5.0f;

    private void Update()
    {
        if (Input.GetKeyDown("e"))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, distance))
            {
                if (!doorOpen)
                {
                    gameObject.transform.Translate(new Vector3(0.0f, 0.0f, 4.0f));
                    doorOpen = true;
                }
                else
                {
                    gameObject.transform.Translate(new Vector3(0.0f, 0.0f, -4.0f));
                    doorOpen = false;
                }
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorOpen : MonoBehaviour {

    public bool openclosed;
    public float angle;


    public bool InDistance;

    private void Start()
    {

        openclosed = false;
    }

    private void OnTriggerEnter(Collider other)
    {
        InDistance = true;
    }
    private void OnTriggerExit(Collider other)
    {
        InDistance = false;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.E) && InDistance == true && openclosed == false)
        {
            openclosed = true;
            transform.Rotate(0, 0, angle);
        }

        else if (Input.GetKeyDown(KeyCode.E) && openclosed == true && InDistance == true)
        {
            openclosed = false;
            transform.Rotate(0, 0, -angle);
        }


    }






}

This is a simple open close door script, no animations 这是一个简单的开关门脚本,没有动画

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

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