- Home /
Enemy units behave normally, until they rotate, then their AI seems to break
I have a turn based strategy game, and the enemy have (very basic)AI. The little units seem to work and behave normally, but once they rotate, their AI seems to blow a fuse and they speed up, go through walls, and teleport randomly across the level.
I have been working on it for the last 2 hours, and had no luck, but as proofreading has always taught me, if you don't pick up on errors, someone else will, so I'm asking anyone to take a look at it and see if I did something wrong.
#pragma strict
var sound1 : AudioClip;
var Attack1 : GameObject;
var MAXHP : float = 100;
var HP : float = 100;
var MAXAP : float = 10;
var AP : int = 10;
var Attackcost : int = 10;
var Attacktype : String;
var isitmyturn : boolean = false;
var isinitiated : boolean = false;
var amIalive : boolean = true;
var mysoldier : GameObject;
var detectionpoint : GameObject;
var backdetectionpoint : GameObject;
var livemodel : GameObject;
var deadmodel : GameObject;
function Update(){
if(isitmyturn == true){
if(isinitiated == false){
if(amIalive == true){
initiatemovement();
isinitiated = true;
}if(amIalive == false){
isitmyturn = false;
}
}
}
if(HP <= 0){
amIalive = false;
livemodel.SetActive(false);
deadmodel.SetActive(true);
}
}
function initiatemovement(){
yield WaitForSeconds(0.2);
var fwd = mysoldier.transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (detectionpoint.transform.position, fwd, 2.1)) {
var randomturn : float = Random.Range(0,100);
if(randomturn > 50){
yield WaitForSeconds(0.2);
mysoldier.transform.Rotate(Vector3(0,90,0));
checkforenemies();
}
if(randomturn <= 50){
yield WaitForSeconds(0.2);
mysoldier.transform.Rotate(Vector3(0,-90,0));
checkforenemies();
}
}
else
{
yield WaitForSeconds(0.2);
mysoldier.transform.Translate(Vector3(0,0,2));
AP -= 1;
}
yield WaitForSeconds(0.2);
checkforenemies();
}
function checkforenemies(){
yield WaitForSeconds(0.2);
var hit : RaycastHit;
var fwd2 = mysoldier.transform.TransformDirection (Vector3.forward);
if (Physics.Raycast(detectionpoint.transform.position, fwd2,hit, 16)){
if(hit.collider.gameObject.CompareTag("Player")){
shoot1();
yield WaitForSeconds(1);
checkforAP();
}
if(hit.collider.gameObject.CompareTag("Untagged")){
checkforAP();
}
}
}
function checkforAP(){
yield WaitForSeconds(0.2);
if(AP < 1){
isitmyturn = false;
isinitiated = false;
yield WaitForSeconds(0.2);
AP = MAXAP;
}
if(AP > 0){
redo();
}
}
function shoot1(){
if(AP >= Attackcost){
if(Attacktype == "single"){
var instance1 : GameObject = Instantiate(Attack1, detectionpoint.transform.position, detectionpoint.transform.rotation);
audio.clip = sound1;
audio.Play();
AP -= Attackcost;
}
if(Attacktype == "burst"){
var instance2 : GameObject = Instantiate(Attack1, detectionpoint.transform.position, detectionpoint.transform.rotation);
audio.clip = sound1;
audio.Play();
yield WaitForSeconds(0.1);
var instance5 : GameObject = Instantiate(Attack1, detectionpoint.transform.position, detectionpoint.transform.rotation);
audio.Play();
yield WaitForSeconds(0.1);
var instance6 : GameObject = Instantiate(Attack1, detectionpoint.transform.position, detectionpoint.transform.rotation);
audio.Play();
AP -= Attackcost;
}
if(Attacktype == "auto"){
var instance26 : GameObject = Instantiate(Attack1, detectionpoint.transform.position, detectionpoint.transform.rotation);
audio.clip = sound1;
audio.Play();
yield WaitForSeconds(0.1);
var instance7 : GameObject = Instantiate(Attack1, detectionpoint.transform.position, detectionpoint.transform.rotation);
audio.Play();
yield WaitForSeconds(0.1);
var instance8 : GameObject = Instantiate(Attack1, detectionpoint.transform.position, detectionpoint.transform.rotation);
audio.Play();
yield WaitForSeconds(0.1);
var instance9 : GameObject = Instantiate(Attack1, detectionpoint.transform.position, detectionpoint.transform.rotation);
audio.Play();
yield WaitForSeconds(0.1);
var instance0 : GameObject = Instantiate(Attack1, detectionpoint.transform.position, detectionpoint.transform.rotation);
audio.Play();
yield WaitForSeconds(0.1);
var instance11 : GameObject = Instantiate(Attack1, detectionpoint.transform.position, detectionpoint.transform.rotation);
audio.Play();
yield WaitForSeconds(0.1);
var instance12 : GameObject = Instantiate(Attack1, detectionpoint.transform.position, detectionpoint.transform.rotation);
audio.Play();
AP -= Attackcost;
}
}
}
function redo(){
if(isitmyturn == true){
if(isinitiated == true){
isinitiated = false;
}}
}
Again, I suspect that the problem arises from lines 59-96. To test the script, place it on a cube that is 1x2x1 dimensionally, and place it at 0,0,0, and place 2x2x2 blocks scattered in front of it on a 2 block grid.
Any help with this problem is appreciated
Your answer
Follow this Question
Related Questions
A* pathfinding avoidance 0 Answers
Obstacle avoidance not working 0 Answers
How to I make FindGameObjectWithTag() not just find itself? 1 Answer
Horde of NavMeshAgents - stops to recalculate path. 4 Answers
Object Avoidance for my enemies 1 Answer