- Home /
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.
Answer by SunnyChow · Sep 15, 2016 at 05:47 AM
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
Answer by iabulko · Sep 15, 2016 at 05:47 AM
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");
}*/
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
RayCasting shows wrong point 1 Answer
Rotation of an object with some delay.. 0 Answers
Multiball Situation in Pong 0 Answers
Make my Gunpoint aim/rotate on the z axis towards my mouse cursor 1 Answer