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.
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");
}*/