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?

alt text

alt text

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!

4 Answers

4

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 Mattias! I was able to use the script, almost without changes. :)

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.

I want to add that this works with a rigidbody, if anyone is looking. //Rotate around X _rigidbody.AddTorque(rotation.x);

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.

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

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!!!