因此,玩家可以在四个方向上移动-向前,向后,向右和向左。但是前进的方向应该是他转动齿轮VR控制器的任何地方。
我尝试了以下代码,但是当我将控制器指向上方时,播放器将向上移动。我也尝试过someQuaternion.y
,但是没有按预期工作。我该怎么办?
Quaternion someQuaternion = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTrackedRemote);
Vector2 dir = GetDirection(OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad, OVRInput.Controller.RTrackedRemote));
if(dir == Vector2.right)
{
transform.Translate(someQuaternion * Vector3.right * currentSpeed * Time.deltaTime);
}
else if(dir == Vector2.left)
{
transform.Translate(someQuaternion * -Vector3.right * currentSpeed * Time.deltaTime);
}
else if (dir == Vector2.up)
{
transform.Translate(someQuaternion * Vector3.forward * currentSpeed * Time.deltaTime);
}
else if (dir == Vector2.down)
{
transform.Translate(someQuaternion * -Vector3.forward * currentSpeed * Time.deltaTime);
}
public Vector2 GetDirection(Vector2 input)
{
Vector2[] directions = new Vector2[] {
Vector2.up,
Vector2.right,
Vector2.down,
Vector2.left
};
Vector2 direction = Vector2.zero;
float max = Mathf.NegativeInfinity;
foreach (Vector2 vec in directions)
{
float dot = Vector2.Dot(vec, input.normalized);
if (dot > max)
{
direction = vec;
max = dot;
}
}
return direction;
}