How to rotate Object with Mouse drag (specifications below)
First of all, I KNOW that this question is already been answered. But what I want to do exactly is rotate an object with mouse…
From the Top view (rotation along Y axis only)
3D, not 2D (I’ll use a basic Perspective camera, not Orthographic)
Including a “speed” variable that I can have rotation control with it (this one just if it’s possible).
C#
Thanks.
**I’ve been trying to adapt other scripts, but I just can’t make it correct. These are four specifications I couldn’t solve and I need together.
Answer by bloeys · Jan 22, 2016 at 04:22 AM
Hey! sorry for the delay, was a bit busy.
So what you actually wanted was a 'look at mouse script', not simply a way to rotate on an axis with speed. Well the solution might look a but complicated but its actually simple, I hope you will get it.
public float rotationSpeed = 8; //This will determine max rotation speed, you can adjust in the inspector
public Camera cam; //Drag the camera object here
void Update()
{
//If you want to prevent rotation, just don't call this method
RotateObject();
}
void RotateObject()
{
//Get mouse position
Vector3 mousePos = Input.mousePosition;
//Adjust mouse z position
mousePos.z = cam.transform.position.y - transform.position.y;
//Get a world position for the mouse
Vector3 mouseWorldPos = cam.ScreenToWorldPoint(mousePos);
//Get the angle to rotate and rotate
float angle = -Mathf.Atan2(transform.position.z - mouseWorldPos.z, transform.position.x - mouseWorldPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, angle, 0), rotationSpeed * Time.deltaTime);
}
So only one thing to note here is that you probably should let the rotation speed be a bit high, because if its a bit low and the mouse rotates too quickly around the object the object will suddenly switch directions, which doesn't look natural, anyway it isn't that much of a problem.
Hope this is what you want :D
O$$anonymous$$G thanks a lot!!!!!!!!!! This is more than EXACTLY what I needed
never thought I would get it, I recognize it
Now trying to averiguate how to mark it as correct answer...
Not at all ;) I have converted to an answer so you can properly mark it. All the best with your projects :)
Hello, what would be the proper way to change this so that it rotates along the Z axis as well?
Answer by bloeys · Jan 13, 2016 at 06:54 PM
Use the values from the mouse axes, Input.GetAxis("Mouse X")
and Input.GetAxis("Mouse X")
, they will tell you how much the mouse has moved on each axis.
You can then combine these with something like transform.Rotate
to make your object rotate on its x&y axes, and I recommend making the rotation relative to world space so its consistent. If you want rotation on the z axis as well then I guess you will need to use another input(There might be a way to do it with the mouse though) like two extra buttons and combine their input with the mouse input.
Speed is simple, just use a speed variable and multiply it by the mouse input, as it is between -1 and 1, if its not just get the sign of the mouse input and multiply by your mouse input.
Hope it helps
Thank you it helps! but I’m not going to solve my problem because I’m not very good at program$$anonymous$$g. I was trying to use this script for now:
https://www.youtube.com/watch?v=CWx$$anonymous$$LkJUxbo
The problem here is that it just works with an Orthographic camera setup because is 2D, If there were just a few lines to change in order to rotate from Prespective camera…
I’m almost ending an entire game ready to publish and I need this so much, so thanks if someone knows it.
I see, no problem then I'll put some code and I'll try to guide you through it the best I can. Now you said that you only want rotation on the y axis, so the code will only take care of that.
public float rotationSpeed; //This will deter$$anonymous$$e max rotation speed, you can adjust in the inspector
public Transform cam; //Drag the camera object here
void Update()
{
//If you want to prevent rotation, just don't call this method
RotateObject();
}
void RotateObject()
{
transform.Rotate(cam.right, Input.GetAxis("$$anonymous$$ouse Y") * rotationSpeed,
Space.World);
}
Now by attaching this script to an object, that object will continuously rotate with the mouse, but only on the y axis. Of course you can put some simple checks to make it only rotate when you want it to.
Feel free to ask anything :)
Thank you so much, this is it!! I didn’t expect that approximate answer, I appreciate it a lot!
But there is just one thing missing, well the most important actually; this rotation is along X axis, and it should rotate along Y axis.
To do so, I’ve changed “cam.right” to “cam.forward”, but you will notice about the problem here… It basically rotates correct on half screen and negative to mouse position at other half screen.
There is just missing something I guess. Thanks again!
Don't mention it :p
Thanks for the pic by the way, I knew from last time that you wanted that axis, and in my tests it did rotate on it, but what I missed is that your camera was top-down, I was testing an ordinary FPS camera. So the fix is quite simple, just keep the latest script, the one that uses the $$anonymous$$ouse X
, but ins$$anonymous$$d of rotating on cam.up
axis, rotate on cam.forward
. That's it!
All the best, and remember to mark something as the answer ;)
Yes I will, but I had already told you about using cam.forward, but still missing something. The problem is that it doesn't make a 360 turn.
Copi-pasted what i said: To do so, I’ve changed “cam.right” to “cam.forward”, but you will notice about the problem here… It basically rotates correct on half screen and negative to mouse position at other half screen.
Thanks
Don't worry about your English, as long as people understand you its enough ;)
So now that the rotation axis is correct, you want to rotate when the mouse drags the object, right?
Yes the axis is correct
Hope this image helps me explaining:
http://s14.postimg.org/fuc1ey7xt/unity_Ans02.jpg
(couldn't upload it as normal)