- Home /
bot AI half working
hi all, i've made this simple script for a walking bot, but i don't know why, sometimes it simply crashes and the gameobject stops moving or doing anything.
here's the script using UnityEngine; using System.Collections;
public class EnemyMoving : MonoBehaviour {
public GameObject player;
public int temp;
public float turnTemp;
public float distanceTemp;
public Vector3 translation;
public GameObject target;
public float distance;
public float maxDistance = 20f;
public float minDistance = 5.0f;
private bool isWalking = false;
private bool isRotating = false;
private float startPointX;
private float startPointY;
private float startPointZ;
private float time0;
public float waitTime;
private bool firstTimeWait = true;
public Quaternion targetRotation;
public Vector3 tempVector3;
private enum states
{
NONE,
WAIT,
TURN,
AHEAD
}
private states currentState;
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player");
}
void Update ()
{
if (currentState == states.AHEAD)
{
GoAhead();
}
else if (currentState == states.TURN)
{
turn();
}
else if (currentState == states.WAIT)
{
Wait();
}
else if (currentState == states.NONE)
{
FirstLevelRandom();
}
}
void FirstLevelRandom()
{
temp = Random.Range(0,3);
switch(temp)
{
case 1:
currentState = states.WAIT;
Debug.Log("wait");
break;
case 2:
currentState = states.TURN;
Debug.Log("turn");
break;
case 3:
currentState = states.AHEAD;
Debug.Log("ahead");
break;
}
}
void Wait()
{
if (firstTimeWait)
{
waitTime = Random.Range(2,8);
firstTimeWait = false;
time0 = Time.time;
}
else
{
float time1 = Time.time;
if (time1 - time0 >= waitTime)
{
currentState = states.NONE;
firstTimeWait = true;
}
}
}
void turn()
{
if (!isRotating)
{
turnTemp = Random.Range(0, 360);
isRotating = true;
targetRotation = Quaternion.Euler(0, turnTemp, 0);
}
else
{
if (transform.rotation.y != targetRotation.y)
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 100f);
}
else
{
currentState = states.NONE;
isRotating = false;
}
}
}
void GoAhead()
{
if (!isWalking)
{
startPointX = transform.position.x;
startPointY = transform.position.y;
startPointZ = transform.position.z;
distanceTemp = Random.Range(10, 50);
tempVector3 = new Vector3 (startPointX, startPointY, startPointZ + distanceTemp);
}
else
{
if (transform.position != tempVector3)
{
transform.Translate(Vector3.forward * Time.deltaTime * 10);
}
else
{
currentState = states.NONE;
isWalking = false;
}
}
}
void FollowPlayer()
{
transform.LookAt(player.transform);
transform.Translate(Vector3.forward * Time.deltaTime);
}
}
i know that the code is not optimized at all, can anyone find the bug here?? thank you very much!!
no, simply anything happens, i've tried it sometimes with debug logs, it passed from one function to another, but at some point, it stopped working, the game was fine, but the AI script stopped changing state, also the random function selector was stopped, like if it was in a state and the function was not complete. this has happened three or four times. besides sometimes when it go into the "goAhead" function, never stops going.
after more debug, i've fixed the "freeze" code, it was the last else condition in the turn function and the missing isWalking = true in the first condition of the goAhead function. But now, the gameobject goes ahead until i stop the game, and it turns randomly in the goAhead function
Your answer
Follow this Question
Related Questions
make an AI Flee 0 Answers
Simple AI Aircraft Script 2 Answers
ai script help. please help this script. 1 Answer
AI fleeing from multiple enemies 0 Answers
Audio Source AI Attractor 2 Answers