- Home /
Slowing Down Rotation Speed
I am trying to get a cube to rotate on button command. I have the button command code down, but the rotation is happening to quick. How do I slow down the rotation speed?
Here is one of the code for the button command rotation( I separated the left and right control from the up and down code)
function Start ()
{
}
function Update() {
if (Input.GetKey ("left")) {
transform.Rotate(0, 90, 0);
}
if (Input.GetKey ("right")) {
transform.Rotate(0, -90, 0);
}
}
Answer by Scribe · Feb 16, 2014 at 03:56 PM
You can slow it down very simply by giving it a value smaller than 90 such as:
transform.Rotate(0, 1, 0);
which would rotate the object at 1 degree per frame. If you have a set 'degrees per second' that you want to rotate by you can use Time.deltaTime to achieve that effect:
transform.Rotate(0, 10*Time.deltaTime, 0);
will rotate at 10 degrees per second.
Scribe
Thank you for the suggestions, but I am trying to rotate the cube 90 degrees every time i press a button.
Then you transform.Rotate is probably not the best method to use. Rotate is designed for continuous rotations, not so much for where you want to rotate a fixed amount. You can still do it with rotate but it becomes more difficult. I answered a similar question to this recently maybe you can make use of the code:
private var startAngle : float;
private var targetAngle : float;
private var startTime : float;
var smooth : float = 1.0; // The number of seconds taken to rotate
private var angle : float;
function Start(){
angle = transform.eulerAngles.y;
}
function Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)){
startTime = Time.time;
startAngle = angle;
targetAngle = targetAngle + 90;
}else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)){
startTime = Time.time;
startAngle = angle;
targetAngle = targetAngle - 90;
}
angle = $$anonymous$$athf.LerpAngle(startAngle, targetAngle, (Time.time - startTime)/smooth);
transform.eulerAngles = Vector3(0, angle, 0);
}
This sets the transform.eulerAngles directly rather than using Rotate.
Scribe
Thank you so much for the script, it was exactly what i was looking for. The only problem I have found with the script is that when i play the game i can rotate the cube left (with left arrow) and right(with the right arrow) but not up and down, I have change the script so that the cube can move up and down but not left or right. Is there any way that i can rotate the cube left, right, up, and down?
When you are rotating in two axis simultaneously it becomes quite a bit harder to calculate the rotation as you don't have a fixed axis to pivot around so I don't think you can extend the previous method to work in 2 axis (though maybe someone can correct me).
I feel like there should be an easier way to do this but I can't think of one though I may investigate using the cross product of the two axis and rotating about that.
Anyway, in the meantime this produces a very smooth rotation that will always finished locked to a multiple of 90degrees.
var targetAngle : Quaternion;
var startAngle : Quaternion;
var forward : Vector3;
var right : Vector3;
var up : Vector3;
private var startTime : float;
function RoundVector(v : Vector3) : Vector3{
return Vector3($$anonymous$$athf.Round(v.x), $$anonymous$$athf.Round(v.y), $$anonymous$$athf.Round(v.z));
}
function RotateAround(vF : Vector3, vR : Vector3, vU : Vector3, axis: Vector3, angle: float) : Quaternion{
var rot: Quaternion = Quaternion.AngleAxis(angle, axis); // get the desired rotation
forward = RoundVector(rot*vF);
right = RoundVector(rot*vR);
up = RoundVector(rot*vU);
startTime = Time.time;
startAngle = transform.rotation;
targetAngle = rot * targetAngle;
}
function Start(){
startAngle = transform.rotation;
targetAngle = startAngle;
forward = RoundVector(transform.forward);
right = RoundVector(transform.right);
up = RoundVector(transform.up);
}
function Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)){
RotateAround(forward, right, up, Vector3.up, -90);
}else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)){
RotateAround(forward, right, up, Vector3.up, 90);
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)){
RotateAround(forward, right, up, Vector3.right, 90);
}else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.DownArrow)){
RotateAround(forward, right, up, Vector3.right, -90);
}
transform.rotation = Quaternion.Slerp(startAngle, targetAngle, Time.time-startTime);
}
Scribe
Thank you so much the script works great and it was exactly what i was looking for.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Object Rotation/Character Speed 1 Answer
How Do I Add speed to my rotation 1 Answer
Array weapons Wheel scroll switch 1 Answer