- Home /
Drag to rotate GameObject
I am building a game where you are rotating some GameObjects. When you select the GameObject, a handle will appear. When you press and drag the handle (or GameObject) I would like to rotate only on the Y-axis the depending on the mouse position. I was thinking of using transform.LookAt but I can´t seem to get it correct. The code below is what I have used now, but it only rotates the GameObject depending on the mouse x position. I would like the player to really have control over the rotation. Any ideas?
void OnMouseDown()
{
mouseReference = Input.mousePosition;
}
void OnMouseDrag()
{
Vector3 offset = (Input.mousePosition - mouseReference);
transform.Rotate(new Vector3(0, offset.x * 0.005f, 0));
}
Yeah, this is awesome. Thank you very much for who has answered and who has asked!
Answer by Mattias-Wargren · Oct 17, 2011 at 06:14 PM
I solved it like this:
using UnityEngine;
using System.Collections;
public class ObjectRotator : 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;
}
}
Thanks for this $$anonymous$$attias! I was able to use the script, almost without changes. :)
You're the man $$anonymous$$attias. That did the trick.
I made the following modification to make it rotate along both axes:
void Update(){
if(_isRotating)
{
// offset
_mouseOffset = (Input.mousePosition - _mouseReference);
// apply rotation
//_rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;
_rotation.y = -(_mouseOffset.x) * _sensitivity;
_rotation.x = -(_mouseOffset.y) * _sensitivity;
// rotate
//transform.Rotate(_rotation);
transform.eulerAngles += _rotation;
// store mouse
_mouseReference = Input.mousePosition;
}
}
Hm my code still has an issue with rotating the cube correctly once you rotate to a Z-axis position. Since it seems to only bother with applying rotation to Y and X axes, it gets really wonky once you get to the Z side and attempt to go left/right/up/down. I'll look into it more and post a solution if I find it.
@justinpatterson - I got around this issue by making a container. I make the container on$$anonymous$$ouseDown, center it to the GameObject, adjust to the camera by making the container.transform to LookAt the camera.transform, then parent the GameObject to the container and rotate the container. On $$anonymous$$ouseUp I release the GameObject from the container. This seems to work well.
I want to add that this works with a rigidbody, if anyone is looking.
//Rotate around X
_rigidbody.AddTorque(rotation.x);
Answer by kromenak · Oct 17, 2011 at 06:57 AM
You might be able to actually track the change in the current drag angle around the object and react accordingly. Something like this:
Vector3 startDragDir;
Vector3 currentDragDir;
Quaternion initialRotation;
float angleFromStart;
void OnMouseDown()
{
startDragDir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
initialRotation = transform.rotation;
}
void OnMouseDrag()
{
currentDragDir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
//gives you the angle in degrees the mouse has rotated around the object since starting to drag
angleFromStart = Vector3.Angle(startDragDir, currentDragDir);
transform.rotation = initialRotation;
transform.Rotate(0.0f, angleFromStart, 0.0f);
}
I haven't actually tested this code, so it might take some tweaking, but I think this would work. I used this sort of code in an iphone game where you rotate things by dragging your finger around an object.
Answer by Jeeva3m · Oct 17, 2016 at 01:57 PM
Hi @MattiasWargren and @justinpatterson Your script works fine for the object rotation on mouse click-drag. I've been trying to control minimum and maximum rotation angles on both x and y. Consider I can rotate and view an objects top view, but dont wanna see the bottom. Same way I can rotate and see front left and right side views but dont wanna rotate to see backside. How to control and set Minimum(x and Y) and Maximum(x and y) angles in the script. (Note: I dont wanna rotate camera rather only object rotation) Please Help
Answer by stbn0 · Apr 25, 2019 at 10:30 PM
Hi @Mattias-Wargren , thanks for this, it's been really helpful
Is it posible to limit the speed if the dragged distance is too high ?
I need to rotate the object but the velocity of the rotation should be limited
Thanks!!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613012306im_/https://answers.unity.com/themes/thub/images/avi.jpg)