Sunday, 9 October 2016

EnemyAI using A* Path Finding

using UnityEngine;
using System.Collections;
using Pathfinding;

[RequireComponent (typeof (Rigidbody2D))]
[RequireComponent (typeof (Seeker))]
public class EnemyAI : MonoBehaviour {


public Transform target;

public Transform enemy;

bool facingRight = true;

public float updateRate = 2f;

private Seeker seeker;

private Rigidbody2D rb;

public Path path;

public float speed = 300f;

public ForceMode2D fMode;

[HideInInspector]
public bool pathIsEnded = false;

public float nextWaypointDistance = 3;

private int currentWaypoint = 0;

private bool searchingForPlayer = false;

public static bool flipface;

// Use this for initialization
void Start () {



seeker = GetComponent<Seeker> ();
rb = GetComponent<Rigidbody2D> ();

if (target == null) {
if(!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine (SearchForPlayer());
}
return;
}

seeker.StartPath (transform.position, target.position, OnPathComplete);

StartCoroutine (UpdatePath ());
}

IEnumerator SearchForPlayer ()
{
GameObject sResult = GameObject.FindGameObjectWithTag ("Player");
if (sResult == null) {
yield return new WaitForSeconds (0.5f);
StartCoroutine (SearchForPlayer ());
} else {
target = sResult.transform;
searchingForPlayer = false;
StartCoroutine (UpdatePath());
return false;
}
}

IEnumerator UpdatePath (){
if (target == null) {
if(!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine (SearchForPlayer());
}
return false;
}

seeker.StartPath (transform.position, target.position, OnPathComplete);

yield return new WaitForSeconds (1f / updateRate);
StartCoroutine (UpdatePath ());
}

public void OnPathComplete (Path p)
{

if (!p.error) {
path = p;
currentWaypoint = 0;
}
}

void FixedUpdate ()
{

if (target == null) {
if(!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine (SearchForPlayer());
}
return;
}

if (path == null) {
return;
}

if (currentWaypoint >= path.vectorPath.Count) {
if (pathIsEnded)
return;


pathIsEnded = true;
return;
}
pathIsEnded = false;

Vector3 dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
dir *= speed * Time.fixedDeltaTime;

rb.AddForce (dir, fMode);

float dist = Vector3.Distance (transform.position, path.vectorPath [currentWaypoint]);
if (dist < nextWaypointDistance) {
currentWaypoint++;
return;
}


if((enemy.position.x > target.position.x) && !facingRight){
Flip();
flipface = false;
}
else if((enemy.position.x < target.position.x) && facingRight){
Flip();
flipface = true;
}
}
private void Flip()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;

// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Bountry" ) {
enemy.GetComponent<CircleCollider2D>().isTrigger = false;
enemy.GetComponent<BoxCollider2D>().isTrigger = false;

}
if (col.gameObject.tag == "Player") {
enemy.GetComponent<CircleCollider2D>().isTrigger = false;
//enemy.GetComponent<BoxCollider2D>().isTrigger = false;
enemy.GetComponent<Rigidbody2D>().AddForce(transform.position * 2);
}

}
void OnCollisionExit2D(Collision2D col)
{
if (col.gameObject.tag == "Bountry" || col.gameObject.tag == "Player") {
enemy.GetComponent<CircleCollider2D>().isTrigger = true;
enemy.GetComponent<BoxCollider2D>().isTrigger = true;
}
}

}

No comments:

Post a Comment