- Home /
Smoothly increase rotation speed ?
Hello! My character controller rotates:
float temp = Input.GetAxis("Horizontal") ;
//left
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
temp -= 50 * Time.deltaTime;
}
//right
else if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
temp += 50 * Time.deltaTime;
}
this._controller.Transform.Rotate(0, temp, 0);
But I want to rotate with smoothly increasing speed! Thank you!
Comment
This would have to be the best written, clearest, most appropriately concise, question posted here for three months! Congratulations! You have shamed 59,000 native English-writers.
Answer by MarkFinn · Oct 05, 2012 at 08:47 AM
This should do it. Tweak the parameters till it suits you.
using UnityEngine;
public class AccelRotate : MonoBehaviour
{
float maxAccel=150, minAccel=.001f;
float accelModR=.001f;
float accelModL=.001f;
float accelRate=0.1f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
float temp = Input.GetAxis("Horizontal");
//left
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
temp -= accelModL * Time.deltaTime;
accelModL+=Time.deltaTime*accelRate;
accelModL=Mathf.Min(accelModL, maxAccel);
}
else
{
accelModL=minAccel;
}
//right
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
temp += accelModR * Time.deltaTime;
accelModR+=Time.deltaTime*accelRate;
accelModR=Mathf.Min(accelModR, maxAccel);
}
else
{
accelModR=minAccel;
}
this.transform.Rotate(0, temp, 0);
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 50), "L : "+accelModL+"\nR : "+accelModR);
}
}
Your answer
