0

I'm working on a Unity project where I need to rotate an object using either the mouse or the controller. However, I've run into an issue: the object keeps rotating with the mouse even when the left mouse button is not pressed, but only when both mouse and controller input methods are enabled at the same time.

Here’s a basic overview of the code:

Mouse Rotation: The object should rotate when the mouse moves, but only when the left mouse button is pressed.

Controller Rotation: The object should also rotate when the right analog stick of the controller is moved.

The Issue:

  • The object rotates unintentionally with the mouse when both the mouse and controller input methods are active.
  • The object should only rotate with the mouse when the left mouse button is pressed.
private void Update()
{
    if (!isInspecting || objectInspect == null) return;

    RotateWithController();
    RotateObject();
}

private void RotateObject()
{
    if (Input.GetMouseButton(0))
    {
        Vector2 mouseDelta = Mouse.current.delta.ReadValue();
        if (mouseDelta != Vector2.zero)
        {
            objectInspect.transform.Rotate(new Vector3(-mouseDelta.y, mouseDelta.x, 0) * Time.deltaTime * rotationSpeed, Space.World);
        }
    }
}

private void RotateWithController()
{
    Vector2 rotationInput = rotateItem.ReadValue<Vector2>();

    if (rotationInput != Vector2.zero)
    {
        objectInspect.transform.Rotate(new Vector3(-rotationInput.y, rotationInput.x, 0) * Time.deltaTime * rotationSpeed * 8f, Space.World);
    }
}
4
  • 1
    So add debug statements, find out whats going on, im going to suggest rotateitem is not returning a 0,0 vector Commented Feb 17 at 18:56
  • 2
    In general things like mouse delta are absolute values and afaik not frame-rate dependent .. so there shouldn't be a reason to use Time.deltaTime on that Commented Feb 17 at 19:31
  • Can you show how your rotateItem action is set up? Make sure that you don't have a mouse binding added to that input action. Commented Feb 18 at 15:54
  • Thanks, it worked! The mouse was bound to the input. I removed it, and the code worked properly. Thanks for your help. Commented Feb 18 at 18:44

1 Answer 1

0

The function RotateWithController() will always rotate the GameObject because there is no condition to stop it from rotating if you press a button on the controller or the mouse.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried but it didn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.