VR看房(player)

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);
    }
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值