Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by alexdevAo · Apr 01, 2014 at 12:22 PM · aienemyfollow

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;
     }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image senc01a · Apr 01, 2014 at 12:36 PM 0
Share

How do you know which waypoints are connected with other waypoints? Can you move from any waypoint to any other waypoint?

avatar image alexdevAo · Apr 02, 2014 at 12:44 AM 0
Share

When I move it looks for nearest waypoint in order to get to me.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges