using UnityEngine;
using System.Collections;
using System;
public class Player : MonoBehaviour {
public static Player instance;
public Transform _rightHand;
public SteamVR_TrackedObject _rightTrackedObj;
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;
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.FindChild("Controller (right)");
_rightTrackedObj = _rightHand.GetComponent<SteamVR_TrackedObject>();
_rightLineR = _rightHand.GetComponent<LineRenderer>();
_rightModel = _rightHand.FindChild("Model");
_leftHand = this.transform.FindChild("Controller (left)");
_leftTrackedObj = _leftHand.GetComponent<SteamVR_TrackedObject>();
_leftLineR = _leftHand.GetComponent<LineRenderer>();
_leftModel = _leftHand.FindChild("Model");
_footTex = GameObject.Find("FootTex").transform;
initQ = _footTex.rotation;
skyBox = this.transform.FindChild("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.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
{
}
if (rightDevice.GetPress(SteamVR_Controller.ButtonMask.Touchpad))
{
if (Physics.Raycast(_rightRay, out _rightHit))
{
_rightLineR.SetPosition(0, _rightModel.position);
_rightLineR.SetPosition(1, _rightHit.point);
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.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
{
}
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.002f, 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;
}
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);
}
}
VR看房(player)
最新推荐文章于 2024-09-17 20:30:37 发布