- Home /
How to rotate 90 degrees from 270 degrees to 0 with lerp? c#
Hi. Im making a small 2d game (that im editing in 3d) and Im currently working on making simple paths that guards can circle in. Im using waypoint-triggers that check the guard for the movement script, set its speed to 0, rotates the guard the desired amount, and sets the guard moving again.
Everything else seems to work fine with my paths, but whenever a waypoint rotates the guard from 270 degrees to 0 degrees, the guard turns the full 270 degrees the long way, instead of going 90 degrees the shorter way. The odd thing about this is, that I can turn the guard from 0 degrees to 270 degrees, and the guard does it the short way with 90 degrees.Any ideas on why is this happening and how to fix it?
Ill post my 2 scripts here: the Waypoint.cs is for the waypoint triggers that give the guard directions, and the WMove.cs is for the guard.
Waypoint.cs
using UnityEngine;
using System.Collections;
public class Waypoint : MonoBehaviour {
public float waitbefore = 0f;
public float rotate = 0f;
public float waitafter = 0f;
public float setforwardspeed = 0f;
public float setsidespeed = 0f;
public GameObject Mover;
public Vector3 settargetAngles; //rotate
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.GetComponent<WMove>() != null) {
Mover = other.gameObject;
WMove WM = other.GetComponent<WMove>();
WM.forwardspeed = 0;
WM.sidespeed = 0;
Debug.Log (other.gameObject.name);
StartCoroutine(corowaitbf());
}
}
IEnumerator corowaitbf(){
yield return new WaitForSeconds(waitbefore);
Debug.Log ("Waitbefore completed!");
WMove WM = Mover.GetComponent<WMove> ();
settargetAngles.z = Mover.transform.eulerAngles.z + rotate;
WM.targetAngles = (settargetAngles);
yield return new WaitForSeconds (waitafter + 3f);
Debug.Log ("Waitafter completed");
WM.forwardspeed = setforwardspeed;
WM.sidespeed = setsidespeed;
}
}
WMove.cs
using UnityEngine;
using System.Collections;
public class WMove : MonoBehaviour {
public float forwardspeed = 0f;
public float sidespeed = 0f;
public Vector3 targetAngles; //rotate
public float smooth = 3f; //rotate
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CharacterController cc = GetComponent<CharacterController> ();
Vector2 speed = new Vector2 (forwardspeed, sidespeed);
speed = transform.rotation * speed;
cc.Move( speed * Time.deltaTime );
//rotate
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, targetAngles, smooth * Time.deltaTime);
if (targetAngles.z < 0) {
targetAngles.z += 360;
}
if (targetAngles.z >= 360) {
targetAngles.z -= 360;
}
if (targetAngles.z - transform.eulerAngles.z > -1 && targetAngles.z - transform.eulerAngles.z < 1) {
//transform.eulerAngles.z = targetAngles.z;
Vector3 temp = transform.eulerAngles;
temp.z = targetAngles.z;
transform.eulerAngles = temp;
//transform.rotation.z = targetAngles.z;
}
//if (transform.eulerAngles = targetAngles) {
// new Vector3 targetAngles
//}
}
}
Answer by robertbu · Aug 01, 2014 at 02:44 PM
Euler angles and Lerp() are always literal. If you tell them to go from 0 to 270, then mathematically they walk from 0 to 270. The solution is to give the angles a mathematical pathway that does what you want. In your case, find the distance from the start angle to the end angle, and if it is greater than 180 degrees, adjust the end angle by either add or subtracting 360. So in the case of 0 to 270, it becomes 360 to 270.
Note my typical solution to this kind of problem is to use Quaternions rather than eulerAngles. Quaternion rotations are always lazy and take the shortest path between two rotations. While it takes some rampup to understand Quaternions, the resulting code is usually shorter and simpler.
Your answer
Follow this Question
Related Questions
Problem about Rotating the Character 1 Answer
How do you smoothly transition/Lerp into a new rotation? 3 Answers
eulerAngles.y always returns 179.9999 1 Answer
Worldspace EulerAngle.x regardless of z rotation ? 3 Answers
Unit rotation fails consistently on all slerp rotations after the first? 0 Answers