- Home /
easing a rotation of ROTATE AROUND
I asked about easing a transform.Rotate function on the other thread http://answers.unity3d.com/questions/22252/how-to-ease-a-rotation
and I got a good "easing" code:
function Update () { if ( Input.GetMouseButtonDown(0)) RotateObject(transform.rotation,Quaternion.Euler(transform.rotation.eulerAngles + Vector3.forward * 90),2.0); }
function RotateObject(startRot : Quaternion, endRot : Quaternion, rotateTime : float) { var i = 0.0; var rate = 1.0/rotateTime; while (i < 1.0) { i += Time.deltaTime * rate; transform.rotation = Quaternion.Lerp(startRot, endRot, Mathf.SmoothStep(0.0, 1.0, i)); yield; } }
but as I went along with the project, I realized,I needed, in fact, an alternative for
transform.RotateAround not for transform.Rotate
and I don't think I understand that code enough, in order to make the rotation rotate around a given point in the space.
Do you think you do?
thank you in advance.
Answer by skovacs1 · Oct 05, 2010 at 05:45 PM
The reason that code works is because you are lerping. The reason you can lerp is that you are rotating in place. RotateAround also changes position so it doesn't quite work the same. There are two simple solutions.
Determine how much of our rotation to perform each time:
function RotateObject(point : Vector3, axis : Vector3,
rotateAmount : float, rotateTime : float) {
var step : float = 0.0; //non-smoothed
var rate : float = 1.0/rotateTime; //amount to increase non-smooth step by
var smoothStep : float = 0.0; //smooth step this time
var lastStep : float = 0.0; //smooth step last time
while(step < 1.0) { // until we're done
step += Time.deltaTime * rate; //increase the step
smoothStep = Mathf.SmoothStep(0.0, 1.0, step); //get the smooth step
transform.RotateAround(point, axis,
rotateAmount * (smoothStep - lastStep));
lastStep = smoothStep; //store the smooth step
yield;
}
//finish any left-over
if(step > 1.0) transform.RotateAround(point, axis,
rotateAmount * (1.0 - lastStep));
}
Slerp the position and rotation separately:
function RotateObject(point : Vector3, axis : Vector3, rotateAmount : float, rotateTime : float) { var rotation = Quaternion.AngleAxis(rotateAmount, axis);
var startPos : Vector3 = transform.position - point;
var endPos : Vector3 = rotation * startPos;
var startRot : Quaternion = transform.rotation;
var step : float = 0.0; //non-smoothed
var smoothStep : float = 0.0; //smoothed
var rate : float = 1.0/rotateTime; //amount to increase non-smooth step by
while(step < 1.0) { // until we're done
step += Time.deltaTime * rate; //increase the step
smoothStep = Mathf.SmoothStep(0.0, 1.0, step);
transform.position = point + Vector3.Slerp(startPos, endPos, smoothStep);
transform.rotation = startRot * Quaternion.Slerp(Quaternion.identity,
rotation, smoothStep);
yield;
}
//finish any left-over
if(step > 1.0) {
transform.position = point + endPos;
transform.rotation = startRot * rotation;
}
}
hmm on a side note, when I try to do something like this:
var sliceCubes:Transform[];
if(Input.Get$$anonymous$$ouseButtonDown(0))
for(var cube : Transform in sliceCubes) cube.RotateObject(centralCube.position, axisOfARotation, rotationAmount, howLongRotation);
it says RotateObject is not a member of UnityEngine.Transform
How to get rid of that problem?