I am trying to rotate an object towards the direction given by de direction of the input. For example, if I am pressing 'up' of the directional pad, the player should rotate pointing that direction.
I've tried using Euler angles, and when I press the direction, I call a coroutine that sums the eulerAngle.y
of the object till it gets the angle of the direction, but it doesn't work. I don't know if there is another approach to use.
Here is the code for the coroutine. It starts if a certain key is pressed and if a flag is not activated:
IEnumerator rotateTowards(float angle)
{
rotating = true;
Debug.Log("Rotating the mono...");
Debug.Log("Your angle: " +transform.eulerAngles.y);
float turningTime = 0.8f;
//float thisAngle = this.transform.eulerAngles.y;
Vector3 thisAngle = this.transform.eulerAngles;
if(thisAngle.y < angle)
{
while(thisAngle.y < angle)
{
thisAngle.y += turningTime * Time.deltaTime;
this.transform.eulerAngles = thisAngle;
}
}
if(thisAngle.y > angle)
{
while(thisAngle.y > angle)
{
thisAngle.y -= turningTime * Time.deltaTime;
this.transform.eulerAngles = thisAngle;
}
}
this.transform.eulerAngles = thisAngle;
rotating = false;
yield return null;
}
Thanks in advance.