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!
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+"
R : "+accelModR);
}
}