Unity 进去区域时显示模型,离开区域时候隐藏模型

1. 在 “Main Camera” 下面创建一个 Cube ,调整大小, 并将其Tag 指定为“Player”(如下图所示)。

2.新建一个脚本,命名为 “Tourcamera” ,用来控制相机的移动(代码如下),并将其挂载在 “Main Camera” 对象上。

using UnityEngine;

/// <summary>导览相机</summary>
public class Tourcamera : MonoBehaviour
{
    private Transform tourCamera;

    public float moveSpeed = 1.0f;
    public float rotateSpeed = 90.0f;
    public float shiftRate = 2.0f;// 按住Shift加速

    private Vector3 direction = Vector3.zero;
    private Vector3 speedForward;
    private Vector3 speedBack;
    private Vector3 speedLeft;
    private Vector3 speedRight;
    private Vector3 speedUp;
    private Vector3 speedDown;


    private void Start()
    {
        if (tourCamera == null) tourCamera = gameObject.transform;
    }

    private void Update()
    {
        if (Application.isEditor)
        {
            getDirection();
            tourCamera.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
        }
    }

    private void getDirection()
    {
        if (Input.GetKeyDown(KeyCode.LeftShift)) moveSpeed *= shiftRate;
        if (Input.GetKeyUp(KeyCode.LeftShift)) moveSpeed /= shiftRate;

        speedForward = Vector3.zero;
        speedBack = Vector3.zero;
        speedLeft = Vector3.zero;
        speedRight = Vector3.zero;
        speedUp = Vector3.zero;
        speedDown = Vector3.zero;

        if (Input.GetKey(KeyCode.W)) speedForward = tourCamera.forward;
        if (Input.GetKey(KeyCode.S)) speedBack = -tourCamera.forward;
        if (Input.GetKey(KeyCode.A)) speedLeft = -tourCamera.right;
        if (Input.GetKey(KeyCode.D)) speedRight = tourCamera.right;
        if (Input.GetKey(KeyCode.E)) speedUp = Vector3.up;
        if (Input.GetKey(KeyCode.Q)) speedDown = Vector3.down;
        direction = speedForward + speedBack + speedLeft + speedRight + speedUp + speedDown;

        if (Input.GetMouseButton(0))
        {
            tourCamera.RotateAround(tourCamera.position, Vector3.up, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime);
            tourCamera.RotateAround(tourCamera.position, tourCamera.right, -Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime);

            direction = v3RotateAround(direction, Vector3.up, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime);
            direction = v3RotateAround(direction, tourCamera.right, -Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime);
        }
    }

    private Vector3 v3RotateAround(Vector3 source, Vector3 axis, float angle)
    {
        Quaternion q = Quaternion.AngleAxis(angle, axis);
        return q * source;
    }
}

 3.新建一个脚本,命名为 “Tags”(代码如下),用来存放所有的标签名字。

public class Tags 
{
    /// <summary>玩家</summary>
    public const string PLAYER = "Player";
}

4.新建一个脚本,命名为 “PlayerTriggeShowAndHide”(代码如下),用来触发隐藏和显示对象。

using UnityEngine;

/// <summary>玩家触发显示和隐藏</summary>
public class PlayerTriggeShowAndHide : MonoBehaviour
{
    [Tooltip("触发作用对象集合")]
    public GameObject[] goArr;

    [Tooltip("是否触发默认事件")]
    public bool defaultEvent = true;
    [Tooltip("是否触发进入事件")]
    public bool enterEvent = true;
    [Tooltip("是否触发离开事件")]
    public bool exitEvent = true;

    [Tooltip("是否触发事件相反执行")]
    public bool contrary = false;


    private void Awake()
    {
       if(defaultEvent) showAndHide(contrary);
    }

    /// <summary>进入区域</summary>
    public void OnTriggerEnter(Collider other)
    {
        if (other.transform.tag.Equals(Tags.PLAYER) && enterEvent)
        {
            showAndHide(!contrary);
        }
    }

    /// <summary>离开区域</summary>
    public void OnTriggerExit(Collider other)
    {
        if (other.transform.tag.Equals(Tags.PLAYER) && exitEvent)
        {
            showAndHide(contrary);
        }
    }

    /// <summary>显示或隐藏对象</summary>
    private void showAndHide(bool show)
    {
        if (goArr != null && goArr.Length != 0)
        {
            for (int i = 0; i < goArr.Length; i++)
            {
                GameObject go = goArr[i];
                if (go != null) go.SetActive(show);
            }
        }
    }
}

5.创建一个“Cube”,勾选 “Is Trigger”,用来制作触发区域。并挂载 “PlayerTriggeShowAndHide” 脚本。

 6. 新建一个 “Sphere”,作为触发显示对象,并赋予到“Cube/PlayerTriggeShowAndHide/goArr” 属性上(如下图所示)。

 7.大功告成!运行并测试。

猜你喜欢

转载自blog.csdn.net/a451319296/article/details/129108475