VIVE手柄移動旋轉(zhuǎn)物體腳本

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("設(shè)置旋轉(zhuǎn)角度,默認為100")]? ? public float setRotateAngle = 100.0f;? ? [Tooltip("設(shè)置上下移動距離,默認為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

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容