- Home /
AI Enemy Follow de player with wayPoints
Hello everyone, I'm making a game like PAC MAN. I already have a script that will be the basis of Intelligence of my enemies.
The script below is the player with some bugs, my enemy does not follow the player properly. It seems that he gets confused where to go. I used waypoints to indicate the path where he must walk to reach the player. Someone can correct and improve the script so that the enemy poses I go the way it should be?
The goal is that it pursues the player as Blinky the PAC MAN Game
AIEnemyBase.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public abstract class AIEnemyBase : MonoBehaviour {
public Transform player;
protected List<GameObject> wayPointsList;
// Use this for initialization
protected void Start () {
player = GameObject.FindGameObjectWithTag("Player").transform;
wayPointsList = new List<GameObject>();
GameObject[] wayPoints = GameObject.FindGameObjectsWithTag("wayPoints");
foreach(GameObject newWayPoints in wayPoints){
wayPointsList.Add(newWayPoints);
}
}
// Update is called once per frame
protected void Update (){
FollowPlayer();
}
// to follow the player
abstract public void FollowPlayer();
}
BlinkyEnemyControl.cs
using UnityEngine;
using System.Collections;
public class BlinkyEnemyControl : AIEnemyBase {
// Use this for initialization
void Start () {
base.Start();
}
// Update is called once per frame
void Update () {
base.Update();
}
public override void FollowPlayer(){
GameObject wayPoint = null;
if(Physics.Linecast(transform.position, player.position)){
// Chamar o metodo para detecção de wayPoints
wayPoint = FindBetterWay();
}else{
// Mover normalmente para destino
wayPoint = GameObject.FindGameObjectWithTag("Player");
}
Vector3 Dir = (wayPoint.transform.position - transform.position).normalized;
transform.position += Dir*Time.deltaTime * speed;
transform.rotation = Quaternion.LookRotation(Dir);
}
GameObject FindBetterWay (){
GameObject betterWay = null;
float distanceToBetterWay = Mathf.Infinity;
foreach(GameObject go in wayPointsList){
float distToWayPoint = Vector3.Distance(transform.position, go.transform.position);
float distWayPointToTarget = Vector3.Distance(go.transform.position, player.position);
float distToTarget = Vector3.Distance(transform.position, player.position);
bool wallBetween = Physics.Linecast(transform.position, go.transform.position);
if((distToWayPoint < distanceToBetterWay) && (distToTarget > distToWayPoint) && (!wallBetween)){
distanceToBetterWay = distToWayPoint;
betterWay = go;
}else{
bool wayPointToTargetCollision = Physics.Linecast(go.transform.position, player.position);
if(!wayPointToTargetCollision){
betterWay = go;
}
}
}
return betterWay;
}
}
How do you know which waypoints are connected with other waypoints? Can you move from any waypoint to any other waypoint?
When I move it looks for nearest waypoint in order to get to me.
Answer by senc01a · Apr 01, 2014 at 02:48 PM
Blinky probably needs to find what is the shortest path among the waypoints in order to reach the player. This is a prototypical problem for which there are plenty of solutions, but the most common used algorithm is A*. A* search Algorithm
A* allows you to find the succession of waypoints that your ghost will have to follow. Once you have found the nearest waypoint to follow, your blinky would simply need to set that as its current goto location, once it gets there you can do again A* to the closest waypoint that the player is to, and proceed this way until you catch the player.
Your answer
Follow this Question
Related Questions
Help with Enemy AI 1 Answer
How To Make Enemy Move Around 0 Answers
car follow ai 1 Answer
Problem with a monster (he follows me backwards) 1 Answer
Enemy Pathing 1 Answer