Rotation Speed

Hi! :slight_smile:
This script make a rotation of 360° of a wheel when I press a key, How can I start/stop gradually the rotation? because so start immediately fast, and it is unrealistic.
Thank you in advance guys, and sorry for my bad english…:wink:

using UnityEngine;
using System.Collections;

public class RotazioneRuota : MonoBehaviour {

    public GameObject rad_main;
    public GameObject[] gondel;

    float rad_rotation;
    float[] gondel_rotation;


void Awake()
{
    

    rad_rotation = 0.0f;

    
    gondel_rotation = new float[24];

    

    for(int i = 0; i<24; i++)
        gondel_rotation _= gondel*.transform.localEulerAngles.z;*_

}

void Update()
{

if (Input.GetKey(KeyCode.A))
* {*

int i;

rad_rotation = (rad_rotation + Time.deltaTime * 5.0f) % 360.0f;

rad_main.transform.localEulerAngles = new Vector3 (0.0f, 0.0f, rad_rotation);

for(i=0;i<24;i++)
gondel.transform.localEulerAngles = new Vector3(0.0f, 0.0f, gondel_rotation - rad_rotation) ;

}

* else if (Input.GetKey(KeyCode.S))*
* {*

int i;

rad_rotation = (rad_rotation + Time.deltaTime * 1.0f) % 360.0f;

rad_main.transform.localEulerAngles = new Vector3 (0.0f, 0.0f, rad_rotation);

for(i=0;i<24;i++)
gondel.transform.localEulerAngles = new Vector3(0.0f, 0.0f, gondel_rotation - rad_rotation) ;

}

}

}

Try using a CoRouting instead of update, utilize a waitforseconds and then have it call itself (so basically its an update now that can have a timer). For example:

using UnityEngine;
using System.Collections;
 
public class RotazioneRuota : MonoBehaviour {
 
    public GameObject rad_main;
    public GameObject[] gondel;
 
    float rad_rotation;
    float[] gondel_rotation;
 
 
void Awake()
{
 
 
    rad_rotation = 0.0f;
 
 
    gondel_rotation = new float[24];
 
 
 
    for(int i = 0; i<24; i++)
        gondel_rotation _= gondel*.transform.localEulerAngles.z;*_

StartCoroutine(rotate());
}

IEnumerator rotate()
{

if (Input.GetKey(KeyCode.A))
{

int i;

rad_rotation = (rad_rotation + Time.deltaTime * 5.0f) % 360.0f;

rad_main.transform.localEulerAngles = new Vector3 (0.0f, 0.0f, rad_rotation);

for(i=0;i<24;i++)
gondel.transform.localEulerAngles = new Vector3(0.0f, 0.0f, gondel_rotation - rad_rotation) ;

}

else if (Input.GetKey(KeyCode.S))
{

int i;

rad_rotation = (rad_rotation + Time.deltaTime * 1.0f) % 360.0f;

rad_main.transform.localEulerAngles = new Vector3 (0.0f, 0.0f, rad_rotation);

for(i=0;i<24;i++)
gondel.transform.localEulerAngles = new Vector3(0.0f, 0.0f, gondel_rotation - rad_rotation) ;

}

}
yield return new WaitForSeconds (.0001f);
StartCoroutine(rotate());
}

That sounds to me like you want to have an acceleration, then you would need another field for the velocity.

float velocity = 0f; //add the field
void Update ()
{
    if (Input.GetKey (KeyCode.A)) 
    {
        velocity = (velocity + Time.deltaTime * 5.0f) % 360.0f; //increase the velocity on key press
    } else if (Input.GetKey (KeyCode.S)) {
        velocity = (velocity + Time.deltaTime * 1.0f) % 360.0f;
    }

    rad_main.transform.localEulerAngles = new Vector3 (0.0f, 0.0f, rad_rotation); //always apply the current rotation

    for (int i=0; i<24; i++)
        gondel <em>.transform.localEulerAngles = new Vector3 (0.0f, 0.0f, gondel_rotation  *- rad_rotation);*</em> 

rad_rotation += velocity * Time.deltaTime; //add the current velocity
//it will rotate by 5 degrees per second at max speed when pressing A and 1 degrees per second when pressing S
velocity = Mathf.Lerp(velocity, 0, Time.deltaTime); //decrease the velocity step by step
}