CEJ Posted May 22, 2014 This thread is for the discussion of creating AI in video games (life), how to learn about it etc. I'm kinda going through the process of learning, and trying things in unity, so I accidentally hijacked this thread. I feel like this topic is potentially large enough to warrant a place, but that will prove self-evident either way. Share this post Link to post Share on other sites
rexor0717 Posted May 23, 2014 For the game I'm making, I use behavior trees to determine AI behavior. Pretty much you have tree nodes that are either conditions, actions, or selectors. Selectors are what determines how the tree and its children are traversed, the conditions return success or failure based off a predefined condition, and actions actually have the behaviors carried out. This is a good introduction to the concept, http://www.altdevblogaday.com/2011/02/24/introduction-to-behavior-trees/. This guy explains it much better than I did. Share this post Link to post Share on other sites
CEJ Posted May 28, 2014 On the campo blog they posted how they did Mecanim Animation Callbacks In Unity. Share this post Link to post Share on other sites
CEJ Posted May 28, 2014 On 5/23/2014 at 5:44 PM, rexor0717 said: For the game I'm making, I use behavior trees to determine AI behavior. Pretty much you have tree nodes that are either conditions, actions, or selectors. Selectors are what determines how the tree and its children are traversed, the conditions return success or failure based off a predefined condition, and actions actually have the behaviors carried out. This is a good introduction to the concept, http://www.altdevblogaday.com/2011/02/24/introduction-to-behavior-trees/. This guy explains it much better than I did. So how would that look in code? I've written some naive faux-code below for condition and action nodes, is that in the ballpark? The selectors seems to glue together everything. I don't have time think about this very carefully right now so sorry for the nonchalance. Condition: void isPlayerVisible(){ If ( player = visible ) return true else If ( player = hidden ) return false } Action: void ShootPlayer(){ Play animation Instantiate bullet } Share this post Link to post Share on other sites
clyde Posted May 30, 2014 Are you aware of the stealth game tutorial project on Unity's site? I haven't tried it myself, I never actually finsihed the space-shooter one, but I imagine that this might be something that would help you out. http://unity3d.com/learn/tutorials/projects/stealth Edit: Oops, wrong thread. Share this post Link to post Share on other sites
Merus Posted May 30, 2014 For a game project I'm doing for uni, I wrote some simple-ish AI for hounds that seems to work pretty well - or, at least, we've had a playtest where there was a lot of tension over whether the player would get away. The player leaves a trail that the hounds follow until they see the player, when they attack and likely kill the player. It's essentially a state machine where the AI either paths towards a target (either the closest trail square, or the player, or it naively follows the trail). Share this post Link to post Share on other sites
PrimeDerektive Posted May 30, 2014 Coincidentally I planned on started coding my AI today for my new game today. Here's a a simple coroutine based state machine I worked up so far. Feel free to borrow #pragma strict //state declaration private enum AIState{ Patrolling, Pursuing, Attacking } //the current AI state var state : AIState = AIState.Patrolling; //interval at which to determine current state var determineStateInterval : float = 0.25; //distance from target to start attacking var attackDistance : float = 3.0; //the maximum distance this unit can sense valid targets var targetCheckDistance : float = 30.0; //the layers to check for potential targets var targetCheckLayers : LayerMask; //the visibility angle the target needs to be in for this unit to "see" it var targetCheckAngle : float = 90.0; //the actual target var target : Transform; function Start(){ //start determining current state every determineStateInterval seconds InvokeRepeating("DetermineState", 0.0, determineStateInterval); } function DetermineState(){ //if we have a target if(target){ //check distance to target var distanceToTarget = Vector3.Distance(target.position, transform.position); //if we're within attack range if(distanceToTarget <= attackDistance){ //start attacking if not already SetState(AIState.Attacking); } //if the target has escaped the awareness distance of target checks else if(distanceToTarget >= targetCheckDistance){ //drop the target, as he has escaped target = null; } //we are not within attack range else{ //start pursuing if not already SetState(AIState.Pursuing); } } //we have no target else{ //check for new targets CheckForTargets(); //if we still have no target if(!target){ //return to patrolling if not already SetState(AIState.Patrolling); } } } function CheckForTargets(){ //we already have a target, no reason to look for more if(target) return; //sweep for target colliders within range var targetColliders = Physics.OverlapSphere(transform.position, targetCheckDistance, targetCheckLayers); //loop through potential targets for (var i = 0; i < targetColliders.Length; i++){ //get direction to target var dirToPotentialTarget = targetColliders[i].transform.position - transform.position; //get angle between forward and target dir var angleToPotentialTarget : float = Vector3.Angle(transform.forward, dirToPotentialTarget); //if the target is within visible angle range if(angleToPotentialTarget <= targetCheckAngle){ //set him as the target target = targetColliders[i].transform; } } } function SetState(newState : AIState){ //we're already in the state we're attempting to move to, return if(state == newState) return; //cancel the last states routine StopCoroutine(state.ToString()); //set new state state = newState; //start new state's routine StartCoroutine(state.ToString()); } function Patrolling(){ //do patrol stuff yield; } function Pursuing(){ //do pursuit stuff yield; } function Attacking(){ //do attack stuff yield; } Share this post Link to post Share on other sites