- Home /
How do I rotate something smoothly?
I want to rotate something smoothly, like an animation. Right now I have
transform.Rotate(0, 0, 30);
and I have tried to look up on how to make it smooth by using
transform.Rotate(new Vector3(0, 0, 30) * Time.deltaTime);
but that never worked for me.
Comment
Answer by toromano · Dec 13, 2015 at 05:08 PM
If you are rotating something constantly with constant amount of degree, there is no point of smoothing. But if you need to rotate smoothly when let's say a button is pressed, you need something like this to achieve what you did with transform.Rotate():
void Start()
{
targetRotation = transform.rotation;
}
void Update()
{
if (Input.GetKeyDown("space"))
targetRotation *= Quaternion.Euler(0, 0, 30);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.time * speed);
}
Hope that helps.
Answer by LiviHJ · Dec 13, 2015 at 07:41 PM
@FroddeB This rotate a cube01 around the y axis at 1 angle(speed)
void Start () {
}
// Update is called once per frame
void Update () {
this.transform.Rotate (new Vector3 (0, 1, 0), 1); //rotates cube 01 around y-axis
}
}
Or, if you want to rotate as you hold a button down:
void Start () {
}
void Update () { //continuously executed while the program is running
if(Input.GetKey(KeyCode.A)){
this.transform.Rotate (new Vector3(0,1,0), 1); //rotation on y-axis
}
}
If you want you can change the angle/speed or direction at which it's turning.