zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Unity便携式 VR 的实现

VR 实现 Unity
2023-09-11 14:19:57 时间

提示:以下是本篇文章正文内容

简介

使用键盘鼠标等按键模便携式VR

效果

在这里插入图片描述

素材

下载链接:链接

代码实现

在这里插入图片描述

1、Singleson单例

using UnityEngine;

public class singleton<T> : MonoBehaviour where T:singleton<T>
{
    private static T instance;

    public static T Instance { get => instance; }

    protected virtual void Awake()
    {
        if (instance != null)
        {
            Destroy(instance);
        }
        else
        {
            instance = (T)this;
        }
    }

}

2、相关UI~UIGmanager

using UnityEngine;
using UnityEngine.UI;

public class UIGmanager : singleton<UIGmanager>
{
    public Text countUI_Text;
    public float returnPosTimer;

    private bool isShow=false;

    public bool IsShow {
        get
        {
            return forwardRay.Instance.IsGoOnMove;
        }
        set
        {
            isShow = !value;
            showCount(isShow);
        }
    }

    private void Start()
    {
    }
    private void Update()
    {
        if (isShow)
        {
            returnPosTimer -= Time.deltaTime;
        }
    }

    void showCount(bool isShow)
    {
        countUI_Text.gameObject.SetActive(isShow);

        if (isShow)
        {
            if (returnPosTimer <0)
            {
                returnPosTimer = 0;
                countUI_Text.gameObject.SetActive(false);
            }
            else
            {
                countUI_Text.gameObject.SetActive(true);
            }
            countUI_Text.text = "移除选择将在"+returnPosTimer.ToString("0") + "秒后会自动归位";
        }
    }
}

3、相关逻辑~orwardRay

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
 
public class forwardRay : singleton<forwardRay>
{
    private Ray ray;
    private RaycastHit hit;
    private RaycastHit hit1;
    private List<Vector3> startPos = new List<Vector3>();
    private List<Vector3> startRos = new List<Vector3>();
    public Transform fatherPos;
    public Image quan;

    private bool isGoOnMove;
    public int index;
    public float LerpSpeed;
    private float lerpSpeedTimer;
    private float LerpSpeedtime = 1f;
    private float lerpStartPosTime=20;
    private float mouseX;
    private float mouseY;
    private float ver;
    private float moveSpeed=100;
    public float mouseSpeed;
    private bool isStartlerp;



    public bool IsGoOnMove { get => isGoOnMove;

        set
        {
           isGoOnMove = value;
            UIGmanager.Instance.IsShow = IsGoOnMove;
        }
    }

    public float LerpSpeedTimer { get => lerpSpeedTimer; set
        {
           lerpSpeedTimer = value;
            UIGmanager.Instance.returnPosTimer = LerpSpeedTimer;
        }
    }


    void Start()
    {
        for (int i = 0; i < fatherPos.childCount; i++)
        {
            startPos.Add(fatherPos.GetChild(i).position);
        }
        for (int i = 0; i < fatherPos.childCount; i++)
        {
            startRos.Add(fatherPos.GetChild(i).eulerAngles);
        }
        LerpSpeedTimer = LerpSpeedtime;
        quan.fillAmount = 0;
    }

    void Update()
    {
        Screen.lockCursor = true;
        mouseX = Input.GetAxis("Mouse X");
        mouseY = Input.GetAxis("Mouse Y");
        ver = Input.GetAxis("Vertical");
        if (transform.position.z >-60 )
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, -60);
        }
        else if (transform.position.z < -90)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, -90);
        }
        else
        {
            transform.position += transform.forward * ver * moveSpeed * Time.deltaTime;
        }

        transform.eulerAngles += Vector3.right * -mouseY * mouseSpeed;
        transform.eulerAngles += Vector3.up * mouseX * mouseSpeed;
        ray = new Ray(transform.position, transform.forward * 10000);


        if (Physics.Raycast(ray,out hit))
        {
            isStartlerp = true;
            Debug.Log("检测到了");
            quan.fillAmount += 0.05f;
            Debug.DrawRay(transform.position, transform.forward * 1000, Color.red);
            Debug.Log(hit.collider.tag);
            switch (hit.collider.tag)
            {
                case "球":
                    index = 0;
                    LerpQiu();
                    break;
                case "兰博基尼":
                    index = 1;
                    LerpPos(index);
                    break;
                case "宝马":
                    index = 2;
                    LerpPos(index);
                    break;
                case "面包车":
                    index = 3;
                    LerpPos(index);
                    break;
            }
            //Debug.Log(Vector3.Distance(fatherPos.GetChild(index).position, transform.position));
            if (Vector3.Distance(fatherPos.GetChild(index).position, transform.position) < 70)
            {
                IsGoOnMove = false;
            }
            else
            {
                IsGoOnMove = true;
            }
            LerpSpeedTimer = LerpSpeedtime;

        }
        else if (isStartlerp) 
        {
            quan.fillAmount = 0;
            IsGoOnMove = false;
            if (!IsGoOnMove)
            {
                if (LerpSpeedTimer <= 0)
                {
                    lerpStartPos(index);
                }
                else
                {
                   LerpSpeedTimer -= Time.deltaTime;
                }
            }
        }

        if (Input.GetKey(KeyCode.R)){
            fatherPos.GetChild(index).transform.Rotate(Vector3.forward);
        } 
        if
         (Input.GetKey(KeyCode.Q))
        {
            fatherPos.GetChild(index).transform.eulerAngles= startRos[index];
        }

    }

    void LerpPos(int index)
    {
      
        //Debug.Log(fatherPos.GetChild(index).position);
        //Debug.Log(transform.position);
        if (IsGoOnMove)
        {
            fatherPos.GetChild(index).position = Vector3.Lerp(fatherPos.GetChild(index).position, 
                new Vector3(transform.position.x,transform.position.y-25f,transform.position.z),
                0.1f * LerpSpeed * Time.deltaTime);
        }
     
    }
    void LerpQiu()
    {
        fatherPos.GetChild(0).GetComponent<MeshRenderer>().material.color = Color.blue;
    }
    void lerpStartPos(int index)
    {
        for (int i = 1; i < fatherPos.childCount; i++)
        {
            if (Vector3.Distance(fatherPos.GetChild(i).position, startPos[i]) >= 1)
            {
                for (int j = 1; j < fatherPos.childCount; j++)
                {
                    fatherPos.GetChild(j).position = Vector3.Lerp(fatherPos.GetChild(j).position,
                    startPos[j],
                    0.1f * lerpStartPosTime * Time.deltaTime);
                }
            }
        }
        fatherPos.GetChild(0).GetComponent<MeshRenderer>().material.color = Color.white;
    }

}