why does my agent keep circling around a previsoulsy set destination?
when you click on the question title thing, it says "type a question relevant to the community"... so i immediately get the urge to ask something like: what is your favorite pizza topping??? now to get it out of the way: yes, I'm a huge noob, i understand what the script means and what goes where but I'm in no way classified to write it on my own off the top of my head (YET). anyway! i have this simple FSM (if it classifies as one) that i fished out from a tutorial on youtube, now, i tell my agent (called behemoth cuz it sounds cool) to set a target after the player is gone, this would be it's "home". so, the player shows up in front of the raycast, the agent charges in his direction, realizes the player is no longer in front of him and decides its time to go home. now up until this point its working, but if you were to stand in front of him again he'll charge but with the home still as its target, so it ends up doing donuts around the home, even though the "charge" state doesn't have a set destination. i spent an entire day trying to rwrite this in different ways (even without an fsm, hardcoding everythin, twas a pain) to no avail. please, if you are kind hearted as to let me know what i did wrong, i apreciate it.
using UnityEngine;
using System.Collections;
public class Controller_Behemoth : MonoBehaviour {
public Transform target;
private NavMeshAgent navComponent;
private bool alive = true;
public float Distance;
private Rigidbody rb;
public int chargeSpeed;
public float cooltime = 3;
//FSM
public State state;
public enum State
{
IDLE,
CHARGING,
RETURNING
}
// Use this for initialization
void Start ()
{
//rb = GetComponent<Rigidbody> ();
//target = GameObject.FindGameObjectWithTag ("Player").transform;
StartCoroutine (FSM ());
navComponent = this.gameObject.GetComponent<NavMeshAgent> ();
state = State.IDLE;
}
IEnumerator FSM()
{
while (alive) {
switch (state) {
case State.IDLE:
idle ();
break;
case State.CHARGING:
charging ();
break;
case State.RETURNING:
returning ();
break;
}
yield return null;
}
}
void idle()
{
}
void Update()
{
RaycastHit hit;
Debug.DrawRay (transform.position, transform.forward, Color.green);
if (Physics.Raycast (transform.position, transform.forward, out hit))
{
if (hit.collider.gameObject.tag == "Player") {
print ("meaw");
state = State.CHARGING;
}
}
}
void returning()
{
if (state == State.RETURNING) {
navComponent.SetDestination (target.position);
} else {
navComponent.SetDestination (this.transform.position);
}
// if (gameObject.transform.position == target.position)
// {
// state = State.IDLE;
// }
}
void charging()
{
cooltime -= Time.deltaTime;
transform.Translate (Vector3.forward * chargeSpeed * Time.deltaTime, Space.Self);
if (cooltime <= 0) {
cooltime = 3;
state = State.RETURNING;
}
}
}