- Home /
Rotate gameobject by mouse at constant speed?
Helo, I want to rotate a game object at a constant speed by holding right-click on it and then dragging the mouse in the direction(only left and right) in which you want to rotate the GO.
if (Input.GetMouseButton(1))
{
gameObject.transform.Rotate(new Vector3(0, -Input.GetAxis("Mouse X"), 0));
}
This is the code I'm using and it works, but if I drag the mouse faster, the rotation will also be faster. I want to rotate the object at a constant speed, no matter how fast I'm dragging the mouse.
Answer by tadadosi · May 26, 2020 at 12:18 PM
You need to store your mouse input and use that to check if you are moving to the right or to the left, and then if you hold right-click and mouseX > 0 or mouseX < 0 do rotate (which is a simple transform.Rotate (axis, angle).
Here is a code that works and also lets you change the axis in the inspector with a simple enum:
using UnityEngine;
public class ConstantRotationUsingMouseInput : MonoBehaviour
{
public enum RotationAxis { x, y, z }
public RotationAxis rotationAxis;
private float mouseX;
private void Update()
{
mouseX = Input.GetAxis("Mouse X");
if (Input.GetMouseButton(1))
{
if (mouseX > 0)
Rotate(-5);
else if (mouseX < 0)
Rotate(5);
}
}
private void Rotate(float rate)
{
Vector3 axis = Vector3.zero;
switch (rotationAxis)
{
case RotationAxis.x:
axis = Vector3.right;
break;
case RotationAxis.y:
axis = Vector3.up;
break;
case RotationAxis.z:
axis = Vector3.forward;
break;
default:
break;
}
transform.Rotate(axis, rate);
}
}
Thanks, using this idea I was able to come up with a solution that worked for me.
if (Input.Get$$anonymous$$ouseButtonDown(1)) {
if (-Input.GetAxisRaw("$$anonymous$$ouse X") > 0)
{
gameObject.transform.Rotate(new Vector3(0, 3.2f, 0));
}
else if (-Input.GetAxisRaw("$$anonymous$$ouse X") < 0)
{
gameObject.transform.Rotate(new Vector3(0, -3.2f, 0));
}
}
Great! Glad I could help :)
Little tip: If you are going to be changing the 3.2f value later, you might wanna add a public or private readonly variable, so it gets easier to edit.
Answer by SteenPetersen · May 26, 2020 at 07:51 AM
What you'll wanna do is lerp. something like this might help (untested, but should give you an idea)
void Update()
{
if (Input.GetMouseButton(1))
{
StartRotation(transform.eulerAngles, new Vector3(0, -Input.GetAxis("Mouse X"), 0));
}
}
private float _timeStartedLerping, lastRotationTime;
private Vector3 _startPosition;
private Vector3 _endPosition;
private bool _isAdjusting;
[SerializeField] float rotSpeed; // speed at which to rotate serialized so we can see it in the inspector
private void StartRotation(Vector3 start, Vector3 end)
{
_isAdjusting = true;
_timeStartedLerping = Time.time;
_startPosition = start;
_endPosition = end;
}
private void FixedUpdate()
{
if (_isAdjusting)
{
float timeSinceStarted = Time.time - _timeStartedLerping;
float percentageComplete = timeSinceStarted / rotSpeed;
transform.eulerAngles = Vector3.Lerp(_startPosition, _endPosition, percentageComplete);
if (percentageComplete >= 1.0f)
{
_isAdjusting = false;
// in case we wish to keep track of this to limit how often we can rotate etc
lastRotationTime = Time.time;
}
}
}
So out of the box this code has a few issues like, always rotating the object a full 360, keeps rotating without the mouse being dragged (rotates when it is just held down) or rotating the object into kind of random positions. After playing around with it for quite a bit I still can't figure it out how things work. Especially the endPosition and the percentageComplete are giving me a headache.