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