Ok this script is fine untill i spawn around 40 enemies at that point the game becomes very laggy, when my units kill some enimes and the numbers drop to around 30 every thing is fine.
I am using Arongranberg’s A* pathfinding because im on a trial unity :(, and i have my node size set to 7 to try and decrease the number of paths.
using UnityEngine;
using System.Collections;
using Pathfinding;
using System.Collections.Generic;
public class EnemyUnit : MonoBehaviour
{
#region Variables
private GameObject EnemySpawnLocations;
private Seeker seeker;
private CharacterController controller;
private Path path;
public float speed;
public float defaultnextWaypointDistance = 3f;
public int currentWaypoint = 0;
private Vector3 diff;
private bool attacking = false;
private bool walking = false;
public float curDistance = 100.0f;
public float CurDistance = 100.0f;
private GameObject[] units;
public bool firstTarget = true;
public float health = 100;
private float hp;
#endregion
// Update and Start
#region Update and Start
void Start()
{
seeker = GetComponent<Seeker> ();
controller = GetComponent<CharacterController> ();
}
void Update()
{
hp = health;
if(hp <= 0)
{
GameObject.Destroy(this.gameObject);
}
if(!walking firstTarget)
{
StartCoroutine(Walk());
} else
if(!walking path.vectorPath[path.vectorPath.Count - 1].z != FindClosestEnemy().transform.position.z)
{
StartCoroutine(Walk());
}else
if(!attacking (Vector3.Distance(transform.position, FindClosestEnemy().transform.position) <= 5))
{
StartCoroutine(Attacking());
}
}
#endregion
//Find Closest Enemy and Attack!
#region FindClosestEnemy
public GameObject FindClosestEnemy()
{
units = GameObject.FindGameObjectsWithTag("Unit");
GameObject closest = null;
float distance = Mathf.Infinity;
distance = Mathf.Infinity;
Vector3 position = this.transform.position;
foreach (GameObject go in units)
{
diff = go.transform.position - position;
curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}return closest;
}
#endregion
//Walk and Attack Numerator IEnumerator
#region IEnumberator
private IEnumerator Walk()
{
Vector3 closest = FindClosestEnemy().transform.position;
firstTarget = false;
walking = true;
seeker.StartPath (transform.position, closest, OnPathComplete);
yield return new WaitForSeconds(2.0F);
walking = false;
}
private IEnumerator Attacking()
{
attacking = true;
FindClosestEnemy().transform.GetComponent<Unit>().Health -= 25;
this.transform.FindChild("Body").animation.Play();
yield return new WaitForSeconds(1.5F);
attacking = false;
}
#endregion
// Pathfinding logic
#region Path Finding and FixedUpdate
public void OnPathComplete(Path p)
{
if(!p.error)
{
path = p;
// reset waypoint
currentWaypoint = 0;
}
}
public void FixedUpdate()
{
if (path == null)
return;
if (currentWaypoint >= path.vectorPath.Count)
return;
// Calculate the direction of the unit
Vector3 dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
dir *= speed * Time.fixedDeltaTime;
controller.SimpleMove (dir); //UNIT MOVEEES!!!
transform.LookAt(new Vector3(path.vectorPath[currentWaypoint].x, transform.position.y, path.vectorPath[currentWaypoint].z));
float nextWaypointDistance = defaultnextWaypointDistance;
if (currentWaypoint == path.vectorPath.Count - 1)
nextWaypointDistance = 0f;
//Check if close enough to the current way point if we are progress to next waypoint
if (Vector3.Distance (transform.position, path.vectorPath [currentWaypoint]) < nextWaypointDistance)// Defined At top
{
currentWaypoint++;
return;
}
}
#endregion
}
Isit my code? the Pathfinding or just my sucky computer with 2.3 Trip core and 4gig of ram
Any help appricated