最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Object Keeps Rotating with Mouse Even When No Code for Rotation is Active unity - Stack Overflow

programmeradmin2浏览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);
    }
}

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);
    }
}
Share Improve this question edited 2 days ago Ferry To 1,4892 gold badges38 silver badges53 bronze badges asked Feb 17 at 18:51 Kaio BarbosaKaio Barbosa 31 bronze badge 4
  • 1 So add debug statements, find out whats going on, im going to suggest rotateitem is not returning a 0,0 vector – BugFinder 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 – derHugo 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. – ipodtouch0218 Commented 2 days ago
  • Thanks, it worked! The mouse was bound to the input. I removed it, and the code worked properly. Thanks for your help. – Kaio Barbosa Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论