1

我正在为一个学校项目开发一个航行模拟器,我目前正在研究船上的气体手柄。它目前正在工作,以便船在把手插入时向前行驶,在把手插入时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

4

1 回答 1

0

你试过移动这条线吗

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

低于这些线?

rotation.y = -( _mouseOffset.y) * _sensitivity;

// rotate
transform.Rotate(_rotation);

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

于 2020-12-02T05:08:08.463 回答