VIVE手柄移动旋转物体脚本

using UnityEngine;

using System.Collections;

public class HandelButtonRight : MonoBehaviour

{

 #region Value File 

 [SerializeField] 

 SteamVR_TrackedObject trackedObj; /// /// 想要移动的物体

    /// [SerializeField]    Transform moveObj;   

 //当前位置   

 float currentPos = 0;    

//距离差    

float disCurrent = 0;    

//Y轴距当前位置   

 float currentPosY = 0;   

 //距离差   

 float disCurrentY = 0;   

 [Tooltip("设置旋转角度,默认为100")]    public float setRotateAngle = 100.0f;    [Tooltip("设置上下移动距离,默认为0.5")]    public float setMoveDistance = 0.5f;   

 #endregion    

#region MonoMathod    

// Use this for initialization    

void Start()    {    }   

 // Update is called once per frame    

void Update()    {        

currentPos = this.transform.position.x; 

       currentPosY = this.transform.position.y;   

     var decive = SteamVR_Controller.Input((int)trackedObj.index);  

      if (decive.GetPress(SteamVR_Controller.ButtonMask.Trigger))  

      {            if (disCurrent > 0.001f)            {                moveObj.Rotate(-Vector3.up * Time.deltaTime * setRotateAngle);            }            else if (disCurrent == 0)            {                return;            }            else if (disCurrent < -0.001f)            {                moveObj.Rotate(Vector3.up * Time.deltaTime * setRotateAngle);            }            //----------------------            if (disCurrentY > 0.005f)            {                moveObj.Translate(Vector3.up * Time.deltaTime * setMoveDistance);            }            else if (disCurrentY == 0)            {                return;            }            else if (disCurrentY < -0.005f)            {                moveObj.Translate(-Vector3.up * Time.deltaTime * setMoveDistance);            }        }    }    void FixedUpdate()    {        disCurrent = this.transform.position.x - currentPos;        disCurrentY = this.transform.position.y - currentPosY;    }    void Awake()    {        trackedObj = GetComponent();

    }

    #endregion

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容