- Home /
Rotate object following mouse movement. Object jumps/ flips
I have a little gnome sitting on a round platform. With a mouse click on the platform I want to rotate the platform around it's Y axis. The angle depends on the position of the mouse.
My implementation is based on the idea of this thread about how to rotate a door according to mouse movement.
Down below is my full script, which is attached to the platform.
The problem: whenever I click on a random point on the platform, the platform will jump/ flip to an unpredictable rotation angle. I tried to prevent that with the rotationStart = transform.rotation
. Unfortunately this won't prevent the platform from flipping. What can I do to achieve a smooth rotation?
EDIT: This is the kind of rotation I am trying to achieve:
The left image displays the rotation behavior when applying robertbu's solution. The red cross symbolizes the mouse click position.
I want the rotation behavior shown on the right image.
Current Code
private var mainCam;
private var firstHitPos;
private var rotationPlane : Plane;
var speed = 0.1;
function Start () {
mainCam = GameObject.FindWithTag("MainCamera");
}
function OnMouseDown(){
var ray = mainCam.camera.ScreenPointToRay(Input.mousePosition);
rotationPlane = new Plane(transform.up, transform.position);
var enter : float;
if(rotationPlane.Raycast(ray , enter)){
firstHitPos = ray.GetPoint(enter);
}
}
function OnMouseDrag(){
var pos = getPlanePos();
var tmpPos = Vector3(pos.x, 0, pos.z);
//var rotationStart= Quaternion.LookRotation(Vector3(firstHitPos.x, 0, firstHitPos.z), transform.up);
var rotationStart= transform.rotation;
var rotationEnd = Quaternion.LookRotation(tmpPos, transform.up);
transform.rotation = Quaternion.Lerp ( rotationStart , rotationEnd, Time.time * speed);
}
function getPlanePos(){
var mousePos = Input.mousePosition;
var ray = mainCam.camera.ScreenPointToRay(mousePos);
var mouseWorldPos = mainCam.camera.ScreenToWorldPoint(Input.mousePosition);
var enter : float;
if(rotationPlane.Raycast(ray , enter)){
var returnVal = mainCam.transform.position + ray.direction.normalized * enter;
return (returnVal);
}
}
Answer by robertbu · Jan 24, 2013 at 03:16 AM
There are a number of problems with the code above. The first is how you are handling your Quaternion.Lerp. Lerp is for rotation over time. You set the start and end rotations, then you pass a fraction between 0 and 1 for where along that rotation you want to be. Typically you do this over time. You are passing Time.time. This is seen frequently in coding examples, but really it is only using the fractional amount in the number of seconds. Your starting point when you call your Lerp for the first time is unknown. And you are resetting the start rotation at every update. Plus I don't think you need or want to do a Lerp in this situation.
You may have special needs with your rotation, but if all you want to do is tie mouse movement to rotation, you can use something simpler like:
private var factor : float = 0.6;
private var v3StartPos : Vector3;
private var v3StartRot : Vector3;
function OnMouseDown(){
v3StartPos = Input.mousePosition;
v3StartRot = transform.eulerAngles;
}
function OnMouseDrag(){
var v3T : Vector3 = Input.mousePosition;
transform.eulerAngles = v3StartRot + Vector3.up * (v3StartPos - v3T).x * factor;
}
I've used a very basic example as a start. This code uses screen units, so you would need to make some additions if you want to run on multiple resolutions and map the same mouse movement as a percentage of the screen to the same rotation.
Thank you very much for your solution suggested solution. Your code works really well. But in order to rotate I have to push my mouse from left to right. I want to have a real-word rotation, so that I have to do circle my mouse in order to rotate. Please check my edited question for further details. Thank you!
Ahhh. I really missed the boat with my answer. A quick and dirty solution is to use the DragRigidbody script from standard assets. Attach a Rigidbody, turn off Use Gravity and freeze everything by y rotation. I'll take another look at your code for the circle rotation when I have a bit more time.
And here is another script that may be what you are looking for:
private var v3StartRot : Vector3;
private var v2Axis : Vector2;
private var fStartAngle : float;
function On$$anonymous$$ouseDown(){
v3StartRot = transform.eulerAngles;
v2Axis = Camera.main.WorldToScreenPoint(transform.position);
var v2T : Vector2 = Input.mousePosition - v2Axis;
fStartAngle = $$anonymous$$athf.Atan2(v2T.y, v2T.x);
}
function On$$anonymous$$ouseDrag(){
var v2T : Vector2 = Input.mousePosition - v2Axis;
var fAngle = $$anonymous$$athf.Atan2(v2T.y, v2T.x);
var v3T : Vector3 = v3StartRot;
v3T.y = v3T.y - (fAngle - fStartAngle) * $$anonymous$$athf.Rad2Deg;
transform.eulerAngles = v3T;
}
Brilliant! This code works as wished! Is there a way to make the rotation in local space? $$anonymous$$eaning the object is rotating about its local y-axis?
I don't have time to test it be sure, but I think you can just replace the two instances of 'eulerAngles' with 'localEulerAngles'.