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 Answer
Reset to default 0The 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.
Time.deltaTime
on that – derHugo Commented Feb 17 at 19:31rotateItem
action is set up? Make sure that you don't have a mouse binding added to that input action. – ipodtouch0218 Commented 2 days ago