纳金网

标题: HTC Vive 实现使用手柄上下拖动 旋转物体的功能 [打印本页]

作者: 烟雨    时间: 2016-7-30 22:45
标题: HTC Vive 实现使用手柄上下拖动 旋转物体的功能
  1. using UnityEngine;
  2. using System.Collections;

  3. public class HandelButtonLeft : MonoBehaviour
  4. {
  5.     #region Value File
  6.     [SerializeField]
  7.     SteamVR_TrackedObject trackedObj;

  8.     /// <summary>
  9.     /// 想要移动的物体
  10.     /// </summary>
  11.     [SerializeField]
  12.     Transform moveObj;

  13.     //当前位置
  14.     float currentPos = 0;
  15.     //距离差
  16.     float disCurrent = 0;

  17.     //Y轴距当前位置
  18.     float currentPosY = 0;
  19.     //距离差
  20.     float disCurrentY = 0;

  21.     [Tooltip("设置旋转角度,默认为100")]
  22.     public float setRotateAngle = 100.0f;
  23.     [Tooltip("设置上下移动距离,默认为0.5")]
  24.     public float setMoveDistance = 0.5f;
  25.     #endregion
  26.     #region MonoMathod

  27.     // Use this for initialization
  28.     void Start()
  29.     {

  30.     }

  31.     // Update is called once per frame
  32.     void Update()
  33.     {

  34.         currentPos = this.transform.position.x;
  35.         currentPosY = this.transform.position.y;

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

  37.         if (decive.GetPress(SteamVR_Controller.ButtonMask.Trigger))
  38.         {
  39.             //moveObj.position = new Vector3(0, this.transform.position.y, 0);
  40.             //moveObj.rotation = Quaternion.LookRotation(this.transform.position);
  41.             //moveObj.Rotate(new Vector3(0, 0, 0));
  42.             if (disCurrent > 0.001f)
  43.             {
  44.                 moveObj.Rotate(-Vector3.up * Time.deltaTime * setRotateAngle);
  45.             }
  46.             else if (disCurrent==0)
  47.             {
  48.                 return;
  49.             }
  50.             else if(disCurrent<-0.001f)
  51.             {
  52.                 moveObj.Rotate(Vector3.up * Time.deltaTime * setRotateAngle);
  53.             }

  54.             //----------------------
  55.             if (disCurrentY>0.005f)
  56.             {
  57.                 moveObj.Translate(Vector3.up * Time.deltaTime * setMoveDistance);
  58.             }
  59.             else if (disCurrentY==0)
  60.             {
  61.                 return;
  62.             }
  63.             else if (disCurrentY < -0.005f)
  64.             {
  65.                 moveObj.Translate(-Vector3.up * Time.deltaTime * setMoveDistance);
  66.             }
  67.         }

  68.     }

  69.     void FixedUpdate()
  70.     {
  71.         disCurrent = this.transform.position.x - currentPos;
  72.         disCurrentY = this.transform.position.y - currentPosY;
  73.     }

  74.     void Awake()
  75.     {
  76.         trackedObj = GetComponent<SteamVR_TrackedObject>();
  77.     }
  78.     #endregion

  79. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5