- Home /
Rotation Speed
Hi! :) 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..;)
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[i] = gondel[i].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[i].transform.localEulerAngles = new Vector3(0.0f, 0.0f, gondel_rotation[i] - 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[i].transform.localEulerAngles = new Vector3(0.0f, 0.0f, gondel_rotation[i] - rad_rotation) ;
}
}
}
Answer by robhuhn · Sep 13, 2013 at 02:47 PM
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 [i].transform.localEulerAngles = new Vector3 (0.0f, 0.0f, gondel_rotation [i] - rad_rotation);
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
}
Answer by Seizure · Sep 13, 2013 at 02:07 PM
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[i] = gondel[i].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[i].transform.localEulerAngles = new Vector3(0.0f, 0.0f, gondel_rotation[i] - 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[i].transform.localEulerAngles = new Vector3(0.0f, 0.0f, gondel_rotation[i] - rad_rotation) ;
}
}
yield return new WaitForSeconds (.0001f);
StartCoroutine(rotate());
}
Your answer
Follow this Question
Related Questions
Uniform rotation - uniform lerp 1 Answer
Set a relative (custom) speed to a rotation 1 Answer
A constant speed rotation finishing with a correct angle. 2 Answers
Adding speed to the rotation every second 1 Answer
Speed limit? 2 Answers