I am completely stumped, very few times in programming have I said that but I can’t seem to figure out how to get my enemy to flee away from my player! I am new to FSM’s and AI behaviors so that might be why, but I get a null reference when this runs, not sure why. I just want a straight forward answer what I am doing wrong, and how I can solve it. I have been trying to figure this out for past couple days-week now.
using UnityEngine;
using System.Collections;
public class FleeScript : MonoBehaviour {
PlayerController PC;
public int nextStateClicker;//change to private
public float FleeTime;
public GameObject target;
public Transform thisTransform; //thisTransform
public float minDistance = 10;
public float runSpeed = 10;
public float maxVel;
// Use this for initialization
void Start ()
{
Debug.Log ("Player has obtained coin, flee mode!");
ChangeStateFlee ();
PC = GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerController> ();
maxVel = 28;
}
void Awake()
{
thisTransform = transform;
thisTransform.position = new Vector3 (2, 0, 2);
}
// Update is called once per frame
void Update () {
}
public enum FleeState
{
FSearch,
FFlee,
}
public FleeState fleeState;
IEnumerator FSearchState()
{
fleeState = FleeState.FFlee;
yield return 0;
}
IEnumerator FFleeState()
{
/*Vector3 distance = targetTrans.position - transform.position;
float updatesAhead = distance.magnitude / maxVel;
Vector3 movePosition = target.transform.position + target.transform.rigidbody.velocity * updatesAhead;
yield return (movePosition);*/
if(Vector3.Distance(PC.GetTransform().position, thisTransform.position) < minDistance)
{
Vector3 direction = thisTransform.position - PC.GetTransform ().position;
direction.Normalize();
thisTransform.position = Vector3.MoveTowards (thisTransform.position, direction * minDistance, Time.deltaTime * runSpeed);
}
yield return 0;
}
/*IEnumerator FleeTimer()
{
} Set Later */
void ChangeStateFlee()
{
StartCoroutine(FSearchState ());
}
void ChangeStateSearch()
{
StartCoroutine(FFleeState());
}
}
Errors: NullReferenceExcetion get_position () and brings me to Vector3.Distance(PC… line