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
//}
}
}