- Home /
Rotate something smoothly by dragging?
Hi,
I am rotating a GameObject OnMouseDrag() using the following script.
void OnMouseDrag() { float mousePosX = Input.GetAxis("Mouse X") * rotationSpeed; transform.Rotate(Vector3.back, -mousePosX, Space.Self); }
I would like to rotate it smoothly so it doesn't come to an abrupt halt as soon as you are not dragging anymore but instead decreases its rotation over time when you stop dragging.
Any help on how to achieve this would be much appreciated.
Thanks in advance
But I would have to change up the script, right? Or is it possible to incorporate it into the existing one using transform.Rotate?
Answer by Hellium · Aug 12, 2019 at 11:20 AM
Get rid of the OnMouseDrag
function, use the following script, and don't forget to add an EventSystem
in your scene (Create > UI > EventSystem), and to attach a raycaster to your camera ( GraphicRaycaster
if you use image, PhysicsRaycaster
if you use 3D objects, Physics2DRaycaster
if you use sprites)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class YourScript : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public float rotationSpeed;
public float rotationDamping;
private float _rotationVelocity;
private bool _dragged;
public void OnBeginDrag(PointerEventData eventData)
{
_dragged = true;
}
public void OnDrag(PointerEventData eventData)
{
_rotationVelocity = eventData.delta.x * rotationSpeed;
transform.Rotate(Vector3.back, -_rotationVelocity, Space.Self);
}
public void OnEndDrag(PointerEventData eventData)
{
_dragged = false;
}
private void Update()
{
if( !_dragged && !Mathf.Approximately( _rotationVelocity, 0 ) )
{
float deltaVelocity = Mathf.Min(
Mathf.Sign(_rotationVelocity) * Time.deltaTime * rotationDamping,
Mathf.Sign(_rotationVelocity) * _rotationVelocity
);
_rotationVelocity -= deltaVelocity;
transform.Rotate(Vector3.back, -_rotationVelocity, Space.Self);
}
}
}
Thanks a lot! It works nicely but somehow @Propagant solution using On$$anonymous$$ouseDrag
looks a little better and smoother no matter how I change the values in your script. Is there a specific reason why I should get rid of On$$anonymous$$ouseDrag
and use yours?
$$anonymous$$y answer uses the EventSystem ins$$anonymous$$d of the (legacy) On$$anonymous$$ouseXXX
function of Unity.
But if Propagant's answer fits your needs, that's perfect ;)
Answer by Propagant · Aug 12, 2019 at 11:18 AM
Hey, try this one:
public float mouseSpeedMultiplier = 8;
public float smoothSpeed = 0.04f;
private float mouseX;
void OnMouseDrag()
{
mouseX += Input.GetAxis("Mouse X") * mouseSpeedMultiplier;
}
void LateUpdate() //Cause we are using Lerp function
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, -mouseX, 0), smoothSpeed);
}
You can also add Y or Z rotation in Quaternion-Euler parameters. Hope that helped!
Your answer
![](https://koobas.hobune.stream/wayback/20220612210518im_/https://answers.unity.com/themes/thub/images/avi.jpg)