Rotation speed?

Hello. When a button is pressed, I want an object to rotate 120 degrees. Currently, the object rotares instantly, how can I have it take a few seconds? And how can I have the affect be compounded, so if the player where the press the rotation button a second time during the few seconds it takes the object to rotate, it would rotate a total of 240 degrees? Here is my code so far. Thanks

if (Input.GetKeyDown("a")) {transform.Rotate (Vector3.up, -120);}

Look into Lerp:

http://unity3d.com/support/documentation/ScriptReference/Quaternion.Lerp.html

But a lot of folks seem to have problems with rotations, so you might want to grab this:

http://www.unifycommunity.com/wiki/index.php?title=Angle

Hi,

Check this fully working project sample on the forum, it has everything you ask for, as well as a sequencer and a manual mode as well. If you have any question, post on that forum thread to keep things organized.

Basically, to give you a short answer, as you press a key, you should only incrementaly rotate your model, so that on each Update you move by a delta amount. Then you implement a check to know when to stop.

Here is the script extracted from the project mentioned above: save this script as a javascript script, drop it on your model, Run and press the left and right keys to rotate your that model.

Warning: the script uses iTween for its automated part, If you don't have this or don't want automated animation, simply comment the function SetTargetValue()

// Description:
//  This script lets you rotate and animate transforms via user inputs or script
//  this is intended for "technical" animation and interaction, but is of course suitable for other subjects
//  it features limits that can be toggled on and off, they are indepedant from the euler angles and relative
//  to the transform itself ( 0 at start )

// what user input will control this transform
public var InputAxe:String="Horizontal";

// at what speed the arm will rotate
public var speed:float=10F;

// around which axis the arm should rotate
// use -1 on the rotation axis if you want to invert the rotation input
public var axeVector:Vector3 = Vector3.up;

// are we limiting the rotation?
public var useLimits:boolean = true;
public var minValue:float = -45F;
public var maxValue:float = 45F;

// private values
// set the inspector to debug to easily see what is the current value
// via script, you can use GetCurrentValue() to retrieve the current value
private var currentValue:float = 0F;
// the delta value of a given update
private var deltaValue:float = 0F;

// are we seeking a target value?
private var seekingTarget:boolean = false;
// the target value, Use SetTargetValue() to set the value to seek
private var targetValue:float = 0F;

// simply return the currentvalue
// use this when you want to know by script where the arm is for example.
function GetCurrentValue(){
    return currentValue;
}

// Use this function to tell the script what value to seek and reach
// you also need to define a time for the anim and a delay
function SetTargetValue(aTarget:float,aTime:float,aDelay:float ){

    // store the target
    targetValue = aTarget;

    // limit the value is necessary
    if (useLimits){
        targetValue = Mathf.Min(Mathf.Max(targetValue,minValue), maxValue);
    }

    // enter seek mode to prevent user inputs while seeking
    seekingTarget = true;

    // build the iTween effect that will return us the current value seeking the target value over time
    iTween.ValueTo(gameObject,{
                                "from":currentValue,
                                "to":targetValue,
                                "time":aTime,
                                "delay":aDelay,
                                "onupdate":"onUpdateFromiTween",
                                "easetype":iTween.EaseType.easeInOutQuad
                                });
    // note on the itween params:
    // onupdate: "onUpdateFromiTween" is a function defined further down this script, itween will pass the current value there
    // Time is the number of seconds it will take to animate the value.
    // delay: defines when it should start
    // easetype: this is what makes the value ease, look in iTween.cs for the exshaustive list

    // full description here: http://itween.pixelplacement.com/documentation.php#ValueTo                        

}// SetTargetValue

function Update () {

    // if we are not seeking a target, we can let the user animate
    if (!seekingTarget){
        UpdateUserInput();
    }

}// Update

function UpdateUserInput(){

    // if we have some input, we can then compute and rotate the transform
    if (Input.GetAxis(InputAxe)){

        // this is the delta value we want to rotate the transform for this frame
        deltaValue = Time.deltaTime * Input.GetAxis(InputAxe) *speed;

        // if we are using limits, we constraint the delta value to the limites
        // note that we are pro active here, we detect and readjust the delta so that it never exceed the limits
        if (useLimits){
            if ( (currentValue+deltaValue) > maxValue){     
                deltaValue = maxValue-currentValue;
            }else if ( (currentValue+deltaValue) < minValue){
                deltaValue = minValue-currentValue;
            }
        }

        // update the current value to keep track of where we are
        currentValue += deltaValue;

        // finally rotate the transform itself
        transform.Rotate(axeVector * deltaValue);

    }

}// UpdateUserInput

// this is a callback from iTween that inform us about the new value to rotate the transform with
function onUpdateFromiTween(aValue:float){

    // we have to work out the delta value now
    deltaValue = aValue - currentValue;
    transform.Rotate(axeVector * deltaValue);

    // the value passed by iTween is the current value
    currentValue = aValue;

    // if we are done, we stop seeking and give back control to the user
    if (currentValue == targetValue){
        seekingTarget = false;
    }

}// onUpdateFromiTween

Hope you'll find this useful.

Bye,

Jean

See the Rotation function here.