如何计算.当旋转由unity 3D中的鼠标位置决定时,在更新中钳制一个轴y



我正在为一个学校项目开发一个航海模拟器,目前我正在研究船的油门。它目前的工作方式是,当手柄处于y axis = 0时,船向前行驶,当手柄位于y axis = 90时,船反向行驶。

目前,我只能通过在检查器中设置值来测试这一点,因为我无法将其夹紧,使其停止在这两个y旋转中。鼠标移动手柄也很好。

我的脚本如下:

public class GasHandleRotator : MonoBehaviour
{
private float _sensitivity;
private Vector3 _mouseReference;
private Vector3 _mouseOffset;
private Vector3 _rotation;
private bool _isRotating;
public AdvancedShipController vc;
public Transform gasHandle;
public Transform forwardTarget;
public Transform reverseTarget;
public static bool forwardTrue = false;
void Start()
{
_sensitivity = 0.4f;
_rotation = Vector3.zero;
}
void Update()
{
if (_isRotating)
{
// offset
_mouseOffset = (Input.mousePosition - _mouseReference);
Vector3 currentRotation = transform.rotation.eulerAngles;
currentRotation.y = Mathf.Clamp(currentRotation.y, 0, 90);
// apply rotation
_rotation.y = -( _mouseOffset.y) * _sensitivity;
// rotate
transform.Rotate(_rotation);
//transform.rotation = currentRotation;
// store mouse
_mouseReference = Input.mousePosition;
}
if (Input.GetMouseButtonDown(1))
{
//forwardTrue = true;
Cursor.lockState = CursorLockMode.None;
// rotating flag
_isRotating = true;
// store mouse
_mouseReference = Input.mousePosition;
}
if (Input.GetMouseButtonUp(1))
{
//forwardTrue = false;
Cursor.lockState = CursorLockMode.Locked;
_isRotating = false;
}
if (gasHandle.rotation == forwardTarget.rotation)
{
vc.input.Throttle = 1.0f;
}
if (gasHandle.rotation == reverseTarget.rotation)
{
vc.input.Throttle = -1.0f;
}
if (Input.GetKeyDown("e"))
{
vc.input.EngineStartStop = true;
}
}
}

我还有一个短视频来说明我如何移动手柄:https://youtu.be/mP1wJ9o4AJw

你试过移动这条线吗

currentRotation.y = Mathf.Clamp(currentRotation.y, 0, 90);

到这些线以下?

rotation.y = -( _mouseOffset.y) * _sensitivity;
// rotate
transform.Rotate(_rotation);

如果我理解正确,应该可以解决你的问题

最新更新