Hi,
so i have been doing my A.I for my enemies and after many trials i can now hit my enemy and it dies including an animation which is awesome now i want it to move again i have achieved this but, what i now get is the allowed range is weird their is a section of my level where i am quite far from the enemy but it decides i am within the allowed range. i then move closer and the allowed range number gets smaller and then jumps to me being further away and it stops moving until i do move into the allowed range and again it starts moving.
what have i done wrong any help always appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpiderAI : MonoBehaviour
{
public GameObject ThePlayer;
public float TargetDistance;
public float AllowedRange = 10;
public GameObject TheEnemy;
public float EnemySpeed;
public int AttackTrigger;
public RaycastHit Shot;
void Update()
{
transform.LookAt(ThePlayer.transform);
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
TargetDistance = Shot.distance;
if (TargetDistance <= AllowedRange)
{
EnemySpeed = 0.05f;
if (AttackTrigger == 0)
{
TheEnemy.GetComponent<Animation>().Play("walk");
transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
}
}
else
{
EnemySpeed = 0;
TheEnemy.GetComponent<Animation>().Play("idle");
}
}
}
}
above is the AI script
Below is the enemy script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpiderEnemy : MonoBehaviour
{
public int EnemyHealth = 10;
public GameObject TheSpider;
public int SpiderStatus;
public int BaseXP = 10;
public int CalculatedXP;
void DeductPoints (int DamageAmount)
{
EnemyHealth -= DamageAmount;
}
// Update is called once per frame
void Update()
{
if (EnemyHealth <= 0 ){
if (SpiderStatus == 0)
{
StartCoroutine(DeathSpider());
}
}
}
IEnumerator DeathSpider()
{
SpiderStatus = 6;
CalculatedXP = BaseXP * GlobalLevel.CurrentLevel;
GlobalXP.CurrentXP += CalculatedXP;
yield return new WaitForSeconds(0.8f);
TheSpider.GetComponent<Animation>().Play("die");
}
}