I’m trying to find a way to stop this twitching bug. Here is my AI Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //LIST
public class AI : Pathfinding {
//public Transform player;
private CharacterController controller;
private bool newPath = true;
public bool moving = false;
public GameObject[] PlayerList;
private GameObject[] AIList;
private LineRenderer lineRenderer;
public Transform SelectedTarget;
public List <Transform> Enemies;
void Start ()
{
SelectedTarget = null;
PlayerList = GameObject.FindGameObjectsWithTag("SelectableUnit");
AddPlayerEnemiesToList();
AIList = GameObject.FindGameObjectsWithTag("Enemy");
lineRenderer = gameObject.GetComponent<LineRenderer>();
}
public void AddTarget(Transform enemy)
{
Enemies.Add(enemy);
}
public void AddPlayerEnemiesToList()
{
foreach(GameObject Player_Enemy in PlayerList)
{
AddTarget(Player_Enemy.transform);
}
}
public void DistanceToTarget()
{
Enemies.Sort(delegate( Transform t1, Transform t2){
return Vector3.Distance(t1.transform.position,transform.position).CompareTo(Vector3.Distance(t2.transform.position,transform.position));
});
}
public void TargetedEnemy()
{
if(SelectedTarget == null)
{
DistanceToTarget();
SelectedTarget = Enemies[0];
}
}
void Update ()
{
TargetedEnemy();
Debug.Log(Enemies[0].position.ToString());
float dist = Vector3.Distance(SelectedTarget.transform.position,transform.position);
//if(dist <150)
//{
//transform.position = Vector3.MoveTowards(transform.position, SelectedTarget.position, 60 * Time.deltaTime);
//}
Vector3 playerPOS = SelectedTarget.transform.position;
if (Vector3.Distance(playerPOS, transform.position) < 25F && !moving && Vector3.Distance(playerPOS, transform.position) > 10F)
{
if (newPath)
{
StartCoroutine(NewPath(playerPOS));
}
moving = true;
}
else if (Vector3.Distance(playerPOS, transform.position) < 2F)
{
//Stop!
//moving = false;
}
else if (Vector3.Distance(playerPOS, transform.position) < 35F && moving)
{
if (Path.Count > 0)
{
if (Vector3.Distance(playerPOS, Path[Path.Count - 1]) > 5F)
{
StartCoroutine(NewPath(playerPOS));
}
}
else
{
if (newPath)
{
StartCoroutine(NewPath(playerPOS));
}
}
//Move the ai towards the player
MoveMethod();
//moving = false;
}
//else
//{
//moving = false;
//}
/*foreach (GameObject p in PlayerList)
{
Vector3 playerPOS = p.transform.position;
if (Vector3.Distance(playerPOS, transform.position) < 25F && !moving && Vector3.Distance(playerPOS, transform.position) > 10F)
{
if (newPath)
{
StartCoroutine(NewPath(playerPOS));
}
moving = true;
}
else if (Vector3.Distance(playerPOS, transform.position) < 2F)
{
//Stop!
//moving = false;
}
else if (Vector3.Distance(playerPOS, transform.position) < 35F && moving)
{
if (Path.Count > 0)
{
if (Vector3.Distance(playerPOS, Path[Path.Count - 1]) > 5F)
{
StartCoroutine(NewPath(playerPOS));
}
}
else
{
if (newPath)
{
StartCoroutine(NewPath(playerPOS));
}
}
//Move the ai towards the player
MoveMethod();
moving = false;
}
//else
//{
//moving = false;
//}
}*/
DrawPath();
}
IEnumerator NewPath(Vector3 playerPOS)
{
newPath = false;
FindPath(transform.position, playerPOS);
yield return new WaitForSeconds(1F);
newPath = true;
}
private void MoveMethod()
{
if (Path.Count > 0)
{
Vector3 direction = (Path[0] - transform.position).normalized;
foreach (GameObject g in AIList)
{
if(Vector3.Distance(g.transform.position, transform.position) < 1F)
{
Vector3 dir = (transform.position - g.transform.position).normalized;
dir.Set(dir.x, 0, dir.z);
direction += 0.2F * dir;
}
}
direction.Normalize();
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, Time.deltaTime * 10F);
if (transform.position.x < Path[0].x + 0.4F && transform.position.x > Path[0].x - 0.4F && transform.position.z > Path[0].z - 0.4F && transform.position.z < Path[0].z + 0.4F)
{
Path.RemoveAt(0);
}
RaycastHit[] hit = Physics.RaycastAll(transform.position + (Vector3.up * 20F), Vector3.down, 100);
float maxY = -Mathf.Infinity;
foreach (RaycastHit h in hit)
{
if (h.transform.tag == "Untagged")
{
if (maxY < h.point.y)
{
maxY = h.point.y;
}
}
}
if (maxY > -100)
{
transform.position = new Vector3(transform.position.x, maxY + 1F, transform.position.z);
}
}
}
private void DrawPath()
{
if (Path.Count > 0)
{
lineRenderer.SetVertexCount(Path.Count);
for (int i = 0; i < Path.Count; i++)
{
lineRenderer.SetPosition(i, Path[i] + Vector3.up);
}
}
else
{
lineRenderer.SetVertexCount(0);
}
}
}
I should add that i want the AI to lock on to the nearest unit so if it’s attacking one and another one comes closer it will switch and attack the new one.