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 wayPointsList;
// Use this for initialization
protected void Start () {
player = GameObject.FindGameObjectWithTag("Player").transform;
wayPointsList = new List();
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;
}
}
↧