当前位置:网站首页>Unity便携式 VR 的实现
Unity便携式 VR 的实现
2022-07-30 01:18:00 【MaNongSa】
提示:以下是本篇文章正文内容
简介
使用键盘鼠标等按键模便携式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;
}
}
边栏推荐
猜你喜欢
News text classification
jar包解压后再打包为jar
CNN的粗浅理解
Minimum number to rotate array
Based on TNEWS 'today's headline news in Chinese short text classification
重建二叉树
Fabric 编写案例 链码
MATLAB被禁下一个会是LABVIEW吗?国产测试软件ATECLOUD崛起发力
高德地图jsapi不生效 INVALID_USER_SCODE
[Flutter] Detailed explanation of the use of the Flutter inspector tool, viewing the Flutter layout, widget tree, debugging interface, etc.
随机推荐
STM32——OLED显示实验
专心致志做事情
Fabric Writing Case Chaincode
How Filebeat ensures that the log file is still correctly read when the log file is split (or rolled)
1592. 重新排列单词间的空格
畅玩西安全攻略
Based on TNEWS 'today's headline news in Chinese short text classification
Linux-安装MySQL(详细教程)
重新定义分析 - EventBridge 实时事件分析平台发布
遇到bug的解决办法,测试再也不背锅了
Minimum number to rotate array
Win11的WSL2系统更换磁盘和wsl使用简介
微信小程序开发之图片压缩方案
Vmtouch - under Linux file cache management artifact
Elephant Swap:借助ePLATO提供加密市场的套利空间
[Experience] Experience Summary - Lessons Learned
The solution to the bug, the test will no longer be blamed
裁员趋势下的大厂面试:“字节跳动”
STM32 - OLED display experiment
【Flutter】Flutter inspector 工具使用详解,查看Flutter布局,widget树,调试界面等