I have a script that rotates an object in the Y axis but how do I rotate the object in the X axis as well? For example, if mouse-drag/touch is moving Left/Right, it should rotate in Y direction and if its moving up/down, it should rotate in X direction.
public class Rotate : MonoBehaviour
{
private float _sensitivity;
private Vector3 _mouseReference;
private Vector3 _mouseOffset;
private Vector3 _rotation;
private bool _isRotating;
void Start()
{
_sensitivity = 0.4f;
_rotation = Vector3.zero;
}
void Update()
{
if (_isRotating)
{
// offset
_mouseOffset = (Input.mousePosition - _mouseReference);
// apply rotation
_rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;
// rotate
transform.Rotate(_rotation);
// store mouse
_mouseReference = Input.mousePosition;
}
}
void OnMouseDown()
{
// rotating flag
_isRotating = true;
// store mouse
_mouseReference = Input.mousePosition;
}
void OnMouseUp()
{
// rotating flag
_isRotating = false;
}
}
Have you tried // apply rotation _rotation.x = -_mouseOffset.x * _sensitivity; _rotation.y = -_mouseOffset.y * _sensitivity;
– Hellium