I’ve got an object that rotates to face the mouse position. I also have a game mechanic that I want to slow the rotation speed down as an integer increases. The closest I’ve come to making it slow down is to divide Time.deltaTime by the integer. However, when I do that it will slow down for a moment as it rotates to face the mouse position and then jerk the object into a new position as the mouse changes position. Any ideas how I can make it smoothly rotate at a slowed rotation speed?
float angle;
void Start () {
StartCoroutine (Rotate ());
}
void FixedUpdate () {
//This gets the angle I want the object to rotate to
Vector3 mousePosition;
Vector3 objectPosition;
mousePosition = Input.mousePosition;
mousePosition.z = 5;
objectPosition = Camera.main.WorldToScreenPoint (transform.position);
mousePosition.x = mousePosition.x - objectPosition.x;
mousePosition.y = mousePosition.y - objectPosition.y;
angle = Mathf.Atan2 (mousePosition.y, mousePosition.x) * Mathf.Rad2Deg;
}
IEnumerator Rotate () {
float moveSpeed = 10f;
while (transform.rotation.z < angle) {
transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.Euler (0, 0, angle), moveSpeed * Time.deltaTime); //If I divide this Time.deltaTime by the integer it slows down the rotation speed like I want it to.
yield return null;
}
myTransform.rotation = Quaternion.Euler (0, 0, angle); //This sets it to the desired angle instantly, but without this when the mouse position changes the object freezes in place.
yield return new WaitForSeconds (0.001f);
StartCoroutine (Rotate ());
}