using UnityEngine;
using System.Collections;
public class RotateAndMoveObject: MonoBehaviour
{
#region Value File
[SerializeField]
SteamVR_TrackedObject trackedObj;
/// <summary>
/// 想要移动的物体
/// </summary>
[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))
{
//moveObj.position = new Vector3(0, this.transform.position.y, 0);
//moveObj.rotation = Quaternion.LookRotation(this.transform.position);
//moveObj.Rotate(new Vector3(0, 0, 0));
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<SteamVR_TrackedObject>();
}
#endregion
}