I am creating an AI player. At this point I am trying to have them do different things depending on the distance the AI is away from the player. From 100 to 50 I want the AI to walk toward the player, next at 50, I want it to decide to walk forward, left, or right. Right now the problem comes at the time of this decision at 50. I have had it print statements for the b4 2nd else if this whole time, its just when it gets to the second else if between mid range and close range that unity just crashes and I have to force quit the application. Below is the code.
Once again, it all crashes at the second if statement in the update. I have no Idea what is wrong. It is not even printing out comments in the second if statement. Please help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class chase : MonoBehaviour {
static Animator anim;
public Transform player;
public float enemySpeed = 0.5f;
public float strafeSpeed = 0.5f;
public float attackdistance = 50;
public float farDistance = 100;
public float midDistance = 50;
public float closeRange = 5;
// Use this for initialization
void Start()
{
//gets possible animations from stored unity animator
anim = GetComponent<Animator>();
}
//start of game logic for AI
// Update AI mode based on enemy distance (far, mid, or close)
void FixedUpdate() {
print("inside update");
// makes AI walk forward due to far distance
//if ((Vector3.Distance(player.position, this.transform.position) <= farDistance) && (Vector3.Distance(player.position, this.transform.position) > midDistance))
if ((floatDistanceBetweenAIPlayer() < farDistance) && (floatDistanceBetweenAIPlayer() > midDistance))
{
print("got through 1st if");
// magnitude of distance from AI to enemy
Vector3 myDirection = vectorDistanceBetweenAIPlayer(player);
//AI looks at player
updateLookAtPlayer(myDirection);
//AI walks toward player
updateWalkForward();
print("iswalkingforward far dist");
print("b4 2nd else if");
}
//makes AI decide to go left, right, forward, jump attack, or idle
//if ((Vector3.Distance(player.position, this.transform.position) <= midDistance) && (Vector3.Distance(player.position, this.transform.position) > closeRange))
else if ((floatDistanceBetweenAIPlayer() < midDistance)&& (floatDistanceBetweenAIPlayer() > closeRange))
{
print("were in 2nd if");
// magnitude of distance from AI to enemy
Vector3 myDirection = vectorDistanceBetweenAIPlayer(player);
print("myDirection has data: " + myDirection);
//AI looks at player
updateLookAtPlayer(myDirection);
print("updateLookAtPlayer(myDirection) has data: ");
//start decision process by generating a random numer
int rnd = Random.Range(1, 3);
print("rando " + rnd);
if (rnd == 1)
{
print("inside if");
updateWalkLeft();
print("INSIDE updateWalkLeft(): ");
}
if (rnd == 2)
{
updateWalkRight();
}
}
/*else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", false);
}*/
}