- Home /
Rotate 90 over time on mouseDown
This is in the main character controller script and is controlled by a ray collision on an update function:
if (Input.GetMouseButtonDown(0)){
miniNode.GetComponent(rotationHandlerLeft).rotateLeftSmall();
}
And this is what I have for moving the rotations right now which works great but Id love for there to be some smooth Animation over time that locks at 90 increments, these are in a seperate script that has no update function at the moment :
function rotateLeftSmall () {
transform.Rotate(Vector3.up*-90); // Rotate to the left
print("Left Rotation");
}
I think this will work but im having trouble with where the updates go I think.
function rotateLeftSmall () {
transform.Rotate(Vector3.up * Time.deltaTime * -90)
}
Answer by Eric5h5 · Apr 06, 2010 at 04:43 PM
Use a coroutine:
private var rotating = false;
function RotateObject (thisTransform : Transform, degrees : Vector3, seconds : float) { if (rotating) return; rotating = true;
var startRotation = thisTransform.rotation;
var endRotation = thisTransform.rotation * Quaternion.Euler(degrees);
var t = 0.0;
var rate = 1.0/seconds;
while (t < 1.0) {
t += Time.deltaTime * rate;
thisTransform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
yield;
}
rotating = false;
}
Which is called like this:
RotateObject(transform, Vector3.up*-90, .5);
Is there anyway to failsafe this, so that It must play out the full duration before the user can click to invoke the function to run again, thus throwing off the 90 increments?
I did what you said, I made this script and is called from another script's "Update" function. The error box says "Update() can not take parameters." Is there something wrong in putting this together?
Wow, this provides the same functionality as about 80 lines of code I wrote. +1 to you sir.
Answer by e-bonneville · Apr 06, 2010 at 04:31 PM
Well, what you want to do is something along these lines:
function Update () {
if (Input.GetMouseButtonDown(0)){
transform.Rotate(Vector3.up * Time.deltaTime * -90);
}
}
This will rotate your object to the left when you press mouse 0. Replace
if (Input.GetMouseButtonDown(0)){
miniNode.GetComponent(rotationHandlerLeft).rotateLeftSmall();
}
with
if (Input.GetMouseButtonDown(0)){
transform.Rotate(Vector3.up * Time.deltaTime * -90);
}
in your Update function.
That won't work, because Get$$anonymous$$ouseButtonDown is called only once, so all it would do is rotate a fraction of the full 90 degrees. Also Ben wanted it to rotate smoothly the full 90 degrees on mouse down, so this is not something you can easily do in Update at all.
Oh, you're right. I lack knowledge of the logic of coding, and although I know the API, I trip up a lot like that. Thanks for pointing that out.