Rotate an object accordingly to the angle from object to mouse position

So I want to rotate a box towards the mouse position.
When I circle the mouse around the box the box should rotate along with the mouse.
I should note that I use orthographic view but with 3d models.

I have tried a lot of different things but I can’t get the result I want. The closest I have come is when I drag the mouse along the X axis the box will rotate. It’s almost what I want and I can work with that.
But as a last resort of getting it exactly as I want I post my problem here.

This is what my latest attempt looks like:

void Update () {
	if (Input.GetMouseButton (0)) {
		if(isRotating == false)
		{
			isRotating = true;
		}

		if(isRotating == true)
		{
			Rotate();
		}
	}
}

void Rotate()
{
	mousePos = Input.mousePosition;
	
	objectPos = _selectObject.selectedObject.transform.position;
	
	Vector3 direction = mousePos - objectPos;
	//Vector3 angle = new Vector3( mousePos, objectPos, 0 );
	
	_selectObject.selectedObject.transform.rotation =  Quaternion.Euler (new Vector3(0f,0f, direction.x));
}

void OnMouseUp(){
	isRotating = false;
	_selectObject.selectedObject.transform.rotation = rotation;
	Debug.Log("UP");
}

It almost follows the mouse when circling around. But jumps back and forth a lot.

2 Answers

2

you can get mouse world position by raycasting to the floor with the mouse screen position.

using the mouse world position of current frame and last frame, you can get the rotation change by

		Quaternion.FromToRotation (lastMousePos-cubeWorldPos,nowMousePos-cubeWorldPos);

and add this rotation change to the cube

cube.transform.rotation = rotationDelta*cube.transform.rotation ;

with this, you should be able to spin the cube with mouse

Probably the best method will be using rays. (I commented OnMouseUp() function - didn’t know that was that doing ;>, and I’m not using isRotating bool):

void Update () {
		if (Input.GetMouseButton (0)) {
			Rotate();
		}
	}
	void Rotate()
	{
		Vector3 pos = Vector3.zero;
		//Create a touch plane that is going through object
		Plane groundPlane = new Plane();
		groundPlane.SetNormalAndPosition(Vector3.up, _selectObject.selectedObject.transform.position);
		//Shooting a ray from camera to a point that mouse is pointing, and getting a point from that ray crossing the plane 
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		float rayDistance;
		if (groundPlane.Raycast(ray, out rayDistance))
			pos = ray.GetPoint(rayDistance);
		//Just let the object look at that direction ;)
		_selectObject.selectedObject.transform.LookAt(pos);
	}
	/*void OnMouseUp(){
		isRotating = false;
		selectedObject.transform.rotation = rotation;
		Debug.Log("UP");
	}*/

Hi, it's pretty simple, they are both private Quaternions of the script. initRotation is the default rotation of your object: 1. if your object is not rotated at all, you can set it to "Quaternion.identity", or remove it. 2. if your object is rotated by, say (90,90,0), you can just set to Quaternion.Euler(90,90,0)" 3. if you want the object to keep the rotation from the editor as default, set it to "transform.localRotation" on Start() transformFromGyro is simply Input.gyro.attitude, but you can use another source , such as another gyro library, or the default C# interface.