Hi,
I’m trying to create a FSM and whilst there aren’t any errors popping up, it isn’t working. The enemy just comes at the player, then starts doing laps around them.
I’m just trying to make the enemy approach where the player is for 2 seconds and then move backwards away from the player for two seconds and then repeat.
using UnityEngine;
using System.Collections;
public class TankScript : MonoBehaviour {
public Transform player;
public float playerDistance;
public float rotationDamping;
public float moveSpeed;
public static bool isPlayerAlive = true;
public Rigidbody tankBull;
public float speed = 20;
public Transform spawnPoint;
public float elapsedTime = 0.0f;
public float shootRate = 3.0f;
public float lastFireTime;
public float fireDelay;
private float time;
GameObject MockronBossFour;
float lockPos = 0;
private Vector3 tankPos;
public enum State {
LookAtPlayer,
Chase,
Back,
};
private State _state;
IEnumerator Start () {
//MockronBossFour = GameObject.Find ("MockronBossFour");
//MockronBossFour.transform.rotation = Quaternion.Euler (transform.rotation.eulerAngles.x, lockPos, transform.rotation.eulerAngles.z);
//tankPos = transform.position;
StartCoroutine ("waitTwoSeconds");
waitTwoSeconds();
StartCoroutine ("waitTwoSecondsAgain");
waitTwoSecondsAgain ();
_state = State.LookAtPlayer;
while (true) {
if (playerDistance < 80f) {
switch (_state) {
case State.LookAtPlayer:
LookAtThePlayer ();
break;
switch (_state) {
case State.Chase:
ChasePlayer ();
break;
switch (_state) {
case State.Back:
EnemyBack ();
break;
yield return 0;
}
}
}
}
}
}
void Update () {
//transform.position = new Vector3 (transform.position.x, 50, transform.position.z);
//transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y, transform.eulerAngles.z);
playerDistance = Vector3.Distance (player.position, transform.position);
}
private void LookAtThePlayer()
{
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationDamping);
if (Time.time > lastFireTime + fireDelay) {
//fire
lastFireTime = Time.time;
}
if (elapsedTime < shootRate) {
elapsedTime++;
} else {
Rigidbody instantiatedTankBull = Instantiate (tankBull, spawnPoint.position, transform.rotation) as Rigidbody;
instantiatedTankBull.velocity = transform.TransformDirection (new Vector3 (0, 0, speed));
elapsedTime = 0;
}
Debug.Log ("Yep");
}
IEnumerator waitTwoSeconds() {
yield return new WaitForSeconds(2);
_state = State.Chase;
}
private void ChasePlayer ()
{
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationDamping);
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
if (Time.time > lastFireTime + fireDelay) {
//fire
lastFireTime = Time.time;
}
if (elapsedTime < shootRate) {
elapsedTime++;
} else {
Rigidbody instantiatedTankBull = Instantiate (tankBull, spawnPoint.position, transform.rotation) as Rigidbody;
instantiatedTankBull.velocity = transform.TransformDirection (new Vector3 (0, 0, speed));
elapsedTime = 0;
}
Debug.Log ("Hepp");
}
IEnumerator waitTwoSecondsAgain() {
yield return new WaitForSeconds(2);
_state = State.Back;
}
private void EnemyBack (){
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationDamping);
transform.Translate (Vector3.back * moveSpeed * Time.deltaTime);
if(Time.time > lastFireTime + fireDelay)
{
//fire
lastFireTime = Time.time;
}
if(elapsedTime < shootRate)
{
elapsedTime++;
}
else{
Rigidbody instantiatedTankBull = Instantiate (tankBull, spawnPoint.position, transform.rotation) as Rigidbody;
instantiatedTankBull.velocity = transform.TransformDirection (new Vector3 (0, 0, speed));
elapsedTime = 0;
Debug.Log ("Mon");
_state = State.Chase;
}
}
}