Unity3D-VR人物瞬移、脚丫旋转判定、高光、开关灯、门、电视
2017-10-31 08:58
971 查看
using UnityEngine; using System.Collections; using System; public class Player : MonoBehaviour { public static Player instance;//静态公有字段 public Transform _rightHand;//右手位置 public SteamVR_TrackedObject _rightTrackedObj;//VR脚本组件 public LineRenderer _rightLineR;//右手线性着色器 public Transform _rightModel;//右手手柄控制器的位置 public Ray _rightRay { get { return new Ray(_rightModel.position, _rightModel.forward); } }//一条从右手手柄发出的射线,指向正前方 public RaycastHit _rightHit;//右手射线检测体 public Transform _leftHand;//左手位置 public SteamVR_TrackedObject _leftTrackedObj;//左手VR脚本组件 public LineRenderer _leftLineR;//左手线性着色器 public Transform _leftModel;//左手手柄位置 public Ray _leftRay { get { return new Ray(_leftModel.position,_leftModel.forward); } }//左手射线,指向正前方 public RaycastHit _leftHit;//左手射线检测体 public Transform _footTex;//脚丫图片位置 public SteamVR_Controller.Device rightDevice;//右手设备控制器 public SteamVR_Controller.Device leftDevice;//左手设备控制 public float lastAngle;//最后的角度 public float currentAngle;//当前的角度 public Quaternion initQ;//初始四元数 public Quaternion resultQ;//结果四元数 public Skybox skyBox;//天空盒 void Awake() { instance = this; _rightHand = this.transform.Find("RightHand"); _rightTrackedObj = _rightHand.GetComponent<SteamVR_TrackedObject>(); _rightLineR = _rightHand.GetComponent<LineRenderer>(); _rightModel = _rightHand.Find("Model");// _leftHand = this.transform.Find("LeftHand"); _leftTrackedObj = _leftHand.GetComponent<SteamVR_TrackedObject>(); _leftLineR = _leftHand.GetComponent<LineRenderer>(); _leftModel = _leftHand.Find("Model"); _footTex = GameObject.Find("foot").transform; initQ = _footTex.rotation; skyBox = this.transform.Find("Camera (head)/Camera (eye)").GetComponent<Skybox>(); InitLeft(); } void Update() { PlayerInput(); } public void PlayerInput() { try { rightDevice = SteamVR_Controller.Input((int)_rightTrackedObj.index); leftDevice = SteamVR_Controller.Input((int)_leftTrackedObj.index); if (rightDevice.GetPress(SteamVR_Controller.ButtonMask.Touchpad)) { _rightLineR.SetPosition(0, _rightModel.position); _rightLineR.SetPosition(1, _rightHit.point); if (Physics.Raycast(_rightRay, out _rightHit)) { Interactiable inter = _rightHit.transform.GetComponent<Interactiable>(); if (inter != null) { inter.HighLighting(); } if (rightDevice.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) { if (inter != null) { inter.Interactive(); } } } else { _rightLineR.SetPosition(0, _rightModel.position); _rightLineR.SetPosition(1, _rightModel.position); } } if (rightDevice.GetPressUp(SteamVR_Controller.ButtonMask.Touchpad)) { _rightLineR.SetPosition(0, _rightModel.position); _rightLineR.SetPosition(1, _rightModel.position); } if (leftDevice.GetPress(SteamVR_Controller.ButtonMask.Touchpad)) { if (Physics.Raycast(_leftRay, out _leftHit, Mathf.Infinity, 1 << 8)) { _footTex.position = _leftHit.point + new Vector3(0, 0.01f, 0); FootTexRotate(); _leftLineR.SetPosition(0, _leftModel.position); _leftLineR.SetPosition(1, _leftHit.point); } else { InitLeft(); } } if (leftDevice.GetPressUp(SteamVR_Controller.ButtonMask.Touchpad)) { if (_leftHit.transform != null && _leftHit.transform.tag == "floor") { this.transform.position = _leftHit.point; } 4000 InitLeft(); } } catch(Exception e) { Debug.Log(e.Message); } } private void FootTexRotate() { //图片旋转 Vector3 playerForward = this.transform.forward; Vector3 rayVector3 = _leftHit.point - new Vector3(_leftModel.position.x, playerForward.y, _leftModel.position.z); currentAngle = Vector3.Angle(playerForward, rayVector3); if(lastAngle != currentAngle) { //判断左右 Vector3 result = Vector3.Cross(playerForward, rayVector3); if (result.y < 0) { Quaternion q = Quaternion.identity; q.eulerAngles = new Vector3(0, -currentAngle, 0); _footTex.rotation = q * initQ; } else { Quaternion q = Quaternion.identity; q.eulerAngles = new Vector3(0, currentAngle, 0); _footTex.rotation = q * initQ; } lastAngle = currentAngle; } } private void InitLeft() { _footTex.position = new Vector3(10000, 10000, 10000); _leftLineR.SetPosition(0, _leftModel.position); _leftLineR.SetPosition(1, _leftModel.position); } }
灯光:
using UnityEngine; using System.Collections.Generic; public class Light_Interactiable : Interactiable { public List<Light> _lights = new List<Light>(); public override void Start() { base.Start(); for (int i = 0; i < this.transform.childCount; i++) { if (this.transform.GetChild(i).childCount>0) { Light light = this.transform.GetChild(i).GetChild(0).GetComponent<Light>(); if (light != null) { _lights.Add(light); } } else { continue; } } SetDefaultState(); } protected override void Open() { for (int i = 0; i < _lights.Count; i++) { _lights[i].enabled = true; } } protected override void Close() { for (int i = 0; i < _lights.Count; i++) { _lights[i].enabled = false; } } }
功能父类:
using UnityEngine; using System.Collections; using HighlightingSystem; public delegate void OnTrigger(Collider col); public class Interactiable : MonoBehaviour { public bool isOpen; public Color highlightingColor = Color.red; public Highlighter highlighter; public OnTrigger triggerEnter; public OnTrigger triggerStay; public OnTrigger triggerExit; public virtual void Start() { highlighter = this.gameObject.AddComponent<Highlighter>(); } public virtual void OnTriggerEnter(Collider col) { triggerEnter(col); } public virtual void OnTriggerStay(Collider col) { triggerStay(col); } public virtual void OnTriggerExit(Collider col) { triggerExit(col); } public virtual void HighLighting() { highlighter.On(highlightingColor); } protected virtual void SetDefaultState() { if(isOpen == true) { Open(); } else { Close(); } } protected virtual void Open() { } protected virtual void Close() { } public virtual void Interactive() { if (isOpen == false) { Open(); } else { Close(); } isOpen = !isOpen; } }
门开关处理:
using UnityEngine; using System.Collections; public enum DoorType { None, Translate, Rotate, } public class Door_Interactiable : Interactiable { public Vector3 closeState; public Vector3 openState; public DoorType doorType; public override void Start() { base.Start(); } void Update() { if(isOpen) { if(doorType == DoorType.Translate) { this.transform.localPosition = Vector3.MoveTowards(this.transform.localPosition, openState, Time.deltaTime); } else if(doorType == DoorType.Rotate) { Quaternion q = Quaternion.identity; q.eulerAngles = new Vector3(0,0, 0); this.transform.rotation = Quaternion.Lerp(this.transform.rotation, q, Time.deltaTime); } } else { if (doorType == DoorType.Translate) { this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, closeState, Time.deltaTime); } else if (doorType == DoorType.Rotate) { Quaternion q = Quaternion.identity; q.eulerAngles = new Vector3(0, -90, 0); this.transform.rotation = Quaternion.Lerp(this.transform.rotation, q, Time.deltaTime); } } } }
电视电影播放:
using UnityEngine; using System.Collections; public class TV_Interactiable : Interactiable { public MovieTexture _movie; public AudioSource _audio; public Texture movie; private MeshRenderer _render; public override void Start() { base.Start(); _render = this.GetComponent<MeshRenderer>(); _render.material.mainTexture = _movie; _audio = this.transform.GetComponent<AudioSource>(); SetDefaultState(); } protected override void Open() { _render.enabled = true; _movie.Play(); _audio.Play(); } protected override void Close() { _render.enabled = false; _movie.Stop(); _audio.Stop(); } }
改变材质:
using UnityEngine; using System.Collections; public enum ChangeMaterialType { None, Skybox, Floor, } public class ChangeMaterialobj_Interactiable : Interactiable { public ChangeMaterialType cmType; public override void Start() { base.Start(); } public override void HighLighting() { base.HighLighting(); } public override void Interactive() { if(cmType == ChangeMaterialType.Skybox) { Player.instance.GetComponent<Skybox>().material = Resources.Load<Material>("SkyBox/Materials/" + this.transform.name); } if(cmType == ChangeMaterialType.Floor) { this.transform.parent.parent.GetComponent<MeshRenderer>().material = Resources.Load<Material>("Floor/Materials/" + this.transform.name); } } }
相关文章推荐
- Unity3d 技巧(11) -VR 瞬移睁眼闭眼 能减少眩晕感
- Unity3D for VR 学习(3): 暴风魔镜PC Input小改造–自己动手、丰衣足食
- unity3d 随机函数 场景中物体移动和旋转
- unity3d Shader镜面高光反射(使用fresenl), bolinPhone是没有物理依据的
- krpano实践之全景图缩放,旋转,倾斜,自动旋转,VR,全屏方法-js调用
- unity3d easytouch计算摇杆旋转角度以及摇杆八方向控制角色
- Unity3d easytouch计算摇杆旋转角度以及摇杆八方向控制角色
- unity3D-游戏/AR/VR在线就业班 C#入门If 语句学习笔记
- Unity3D 人物移动 摄像机跟随
- unity3D 物体 旋转
- unity3d开发一:入门知识+导入模型+旋转脚本
- 三分钟用Unity3D开发第一个HTC VIVE的VR程序
- 时光煮雨 Unity3D实现2D人物移动-总结篇
- [unity3d]汽车的开关门动画播放
- Unity3D for VR 学习(10): Unity LOD Group 组件
- 【Unity3D】学习笔记(第4记) 2D游戏开发基本技巧之人物动画
- Unity3D 游戏贴图(法线贴图,漫反射贴图,高光贴图)
- Unity3D的相机跟随、视野缩放以及旋转
- u3d 人物跟着鼠标旋转
- Unity3D for VR 学习(7): 360°全景照片