安住手柄的trigger键,然后移动手柄,判断是从左到右,从右到左,从上到下,从下到上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TranslateAndRotateCtrl : MonoBehaviour {
Vector3 first = Vector3.zero; //记录鼠标点击的初始位置
Vector3 second = Vector3.zero; //记录鼠标移动时的位置
/// <summary>
/// 最低高度
/// </summary>
public float LimitMinY = -2.8f;
/// <summary>
/// 最高高度
/// </summary>
public float LimitMaxY = 8f;
bool dragging = false; //标记是否鼠标在滑动
[SerializeField]
SteamVR_TrackedObject trackedObj;
[Tooltip("设置旋转角度,默认为100")]
public float setRotateAngle = 150.0f;
[Tooltip("设置上下移动距离,默认为0.5")]
public float setMoveDistance = 2f;
public Transform rotatorObj;
public Transform translateObj;
public bool isOpenRotate = true;
bool isChangeUpAndDown = true;
private void Start()
{
}
void Update()
{
var device = SteamVR_Controller.Input((int)trackedObj.index);
if (device == null) return;
//if (decive.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
//{ //记录鼠标按下的位置
// MainUi.Instance.ShowMenu();
//}
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
{ //记录鼠标按下的位置
first = device.transform.pos;
}
dragging = false;
if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
{ //记录鼠标拖动的位置
dragging = true;
second = device.transform.pos;
Vector3 slideDirection = second - first;
float x = slideDirection.x, y = slideDirection.y, z = slideDirection.z;
if (Mathf.Abs(y) > Mathf.Abs(x) && Mathf.Abs(y) > Mathf.Abs(z))
{
//上下
if (y > 0)
{
// Debug.Log("down");
if (isChangeUpAndDown)
{
translateObj.Translate(Vector3.up * Time.deltaTime * setMoveDistance);
if (translateObj.localPosition.y >= LimitMaxY)
translateObj.localPosition = new Vector3(translateObj.localPosition.x, LimitMaxY, translateObj.localPosition.z);
}
else
{
translateObj.Translate(-Vector3.up * Time.deltaTime * setMoveDistance);
if (translateObj.localPosition.y <= LimitMinY)
translateObj.localPosition = new Vector3(translateObj.localPosition.x, LimitMinY, translateObj.localPosition.z);
}
}
else
{
// Debug.Log("up");
if (isChangeUpAndDown)
{
translateObj.Translate(-Vector3.up * Time.deltaTime * setMoveDistance);
if (translateObj.localPosition.y <= LimitMinY)
translateObj.localPosition = new Vector3(translateObj.localPosition.x, LimitMinY, translateObj.localPosition.z);
}
else
{
translateObj.Translate(Vector3.up * Time.deltaTime * setMoveDistance);
if (translateObj.localPosition.y >= LimitMaxY)
translateObj.localPosition = new Vector3(translateObj.localPosition.x, LimitMaxY, translateObj.localPosition.z);
}
}
}
else if (Mathf.Abs(x) > Mathf.Abs(y) || Mathf.Abs(z) > Mathf.Abs(y))
{
//左右
if (!isOpenRotate) return;
if (Mathf.Abs(x) > Mathf.Abs(z)) //X轴上变化
{
if (x > 0)
{
Debug.Log("right");
rotatorObj.Rotate(-Vector3.up * Time.deltaTime * setRotateAngle);
}
else
{
Debug.Log("left");
rotatorObj.Rotate(Vector3.up * Time.deltaTime * setRotateAngle);
}
}
else //z轴上变化
{
if (z > 0)
{
Debug.Log("right");
rotatorObj.Rotate(-Vector3.up * Time.deltaTime * setRotateAngle);
}
else
{
Debug.Log("left");
rotatorObj.Rotate(Vector3.up * Time.deltaTime * setRotateAngle);
}
}
}
}
}
}