- Home /
Rotate a set amount over time WITHOUT lerping
Hi
I am working on a grid based movement and I am having some problems with rotating. Basically I want to rotate 90 degrees when you press left or right. I've been searching for a solution all day since I can't get my own version to work. This is my code so far.
public Vector3 moveDirection, moveDest;
public bool moving, rotating;
public float rotationSpeed = 1;
// Update is called once per frame
void Update()
{
moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
//Debug.Log(moveDirection);
if (!moving)
{
if (moveDirection == new Vector3(-1.0f, 0, 0))
{
Debug.Log("Turn left");
moving = true;
rotating = true;
moveDest = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y - 90, transform.eulerAngles.z);
StartCoroutine(RotateMe(moveDest, rotationSpeed));
}
if (moveDirection == new Vector3(1.0f, 0, 0))
{
Debug.Log("Turn right");
moving = true;
rotating = true;
moveDest = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y + 90, transform.eulerAngles.z);
StartCoroutine(RotateMe(moveDest, rotationSpeed));
}
if (moveDirection == new Vector3(0, 0, 1))
{
Debug.Log("Move Forward");
moving = true;
rotating = false;
}
if (moveDirection == new Vector3(0, 0, -1))
{
Debug.Log("Turn around");
moving = true;
rotating = true;
moveDest = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y + 180, transform.eulerAngles.z);
StartCoroutine(RotateMe(moveDest, rotationSpeed));
}
}
}
IEnumerator RotateMe(Vector3 angles, float time)
{
Quaternion fromAngle = transform.rotation;
Quaternion toAngle = Quaternion.Euler(transform.eulerAngles + angles);
for (float i = 0; i < 1; i += Time.deltaTime/time)
{
transform.rotation = Quaternion.Lerp(fromAngle, toAngle, i);
yield return null;
}
moving = false;
}
The IEnumarator is from another answer found here somewhere but I can't not seem to find it again.
So this code works the first time but then it screws up in weird ways. Right now I just want to fix the lerp problem. When I lerp the rotation ends on numbers like 89.4921. Now this might not seem bad but when getting a new number it can turn into 178.8382 and so on. But for some reason that I can't detect the numbers get really messed up. But that's for another problem.
So is there a way I can smoothly rotate my player and still set the rotation to 0, 90, 180 and 270?
Thanks in advance!
If you understate i, i.e the i < 1 condition in the for loop, at the moment it crosses 1 the for code stops running. This means the Lerp never reaches 1 and never reaches toAngle. If you can make it run for just 1 more frame this will be prevented.
If my memory serves me correctly, a do while loop will achieve this as the do codeblock will run one last time when the while conidtion is fulfilled [citation needed] :)
@meat5000 I am not sure if this is what you mean but whatever I did, it didn't work. I replaced the for loop with this:
while(transform.eulerAngles != angles)
{
transform.rotation = Quaternion.Lerp(fromAngle, toAngle, rotationSpeed * Time.deltaTime);
yield return null;
}
I don't think a do-while loop (ihttps://msdn.microsoft.com/en-us/library/370s1zax.aspx) will help since the main difference from a normal while loop is that it executes at least once, not an extra time.
Seems easist to just set the rotation after the last yield, i.e.
IEnumerator Rotate$$anonymous$$e(Vector3 angles, float time)
{
Quaternion fromAngle = transform.rotation;
Quaternion toAngle = Quaternion.Euler(transform.eulerAngles + angles);
for (float i = 0; i < 1; i += Time.deltaTime/time)
{
transform.rotation = Quaternion.Lerp(fromAngle, toAngle, i);
yield return null;
}
transform.rotation = toAngle.
moving = false;
}
If you want to wait another frame then add another yield return null after setting the rotation.
Ah yes wasnt sure and didnt have time to look it up. Thanks for that.
@Real$$anonymous$$TG what I mean is, in
for (float i = 0; i < 1; i += Time.deltaTime/time)
i will never be exactly equal to 1, it will always be under. Because of this the Lerp t factor will never actually reach 1 and so will always fall short of toAngle.