- Home /
AI Script for tag like game
I have a small game (like tag). I have multiple bots that wander on the map. At any given time the nearest bot should chase the player and if tagged then the player loses. The catch is only one bot (the nearest) can be n active pursuit at one time.
Any thoughts?
Hmm... you proberly should use raycasting. When the player is in range,the enemy persuits.
@DanishDude: What does raycasting have to do with deter$$anonymous$$ing whether the player is in range?
Raycasting, is just like a "lazer beam" as you see in movies inside a banks vault. In this case, if the player comes in range, and hits the so called "beam" casted by the raycast, the player is in range.
Answer by The_r0nin · Dec 18, 2010 at 03:15 PM
Make an empty gameobject (call it the "Warden" or something [little pun there]). On it put a script with a static variable (call it isChasing:GameObject or something) for who is chasing and a variable for the shortest distance.
Then on each enemy, cache the Warden object in a variable on Awake() (it'll save processing speed). Every .5 second or so (why do it every frame?) have the enemies check distance. If the bot's distance is less than the stored distance it sets a variable on itself to start chasing, and sends its distance to the Warden. Then Warden then sends a message to isChasing to stop chasing, and replaces the object in isChasing with the new bot and the new distance.
This way isn't the best, but it is a good way to get started. Right at the very beginning, there will be a moment when most of the bots think they are chasing (as the distance is whittled down), but then it should work like you want it to.
Oh, and to check distance, use Vector3.Distance(enemy.transform.position, player.transform.position);
To be clear, I am attaching the "warden" < good pun by the way, to the main player?
Also to prevent that moment of confusion for the bots would it benefit me in any way to assign one bot as the starting bad guy?
ALSO pardon me if my questions seem basic but scripting is a week side for me and this is for a school assignment and I am going FAR above the requirements of the project.
No, the warden is an empty game object (so that if you destroy the player object, you don't have to reset variables). You could do this on the player, if you wanted, though. And a starting bad guy would only help if he was closest to the spawn point. Staring with a default distance might help, but it means no one would chase until the first bot got within that distance.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091522im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animay play in editor 0 Answers
AI Script attached to Enemy and is Rotating around player 0 Answers
Fast Script Ai For mobile: Navmesh or A*? 1 Answer
C# Script Not Working? 2 Answers