- Home /
AI for 3rd person shooter
how do you create artificial intelligence for a 3rd person shooter game... preferably an enemy which walks and shoots once in range??
Answer by FTheCloud · Aug 05, 2011 at 05:08 AM
First start with a path-finding system so the enemy walks towards the player:
http://www.arongranberg.com/unity/a-pathfinding/
Then write a script that measures the distance between the enemy and the player. Ex.
var distance = Vector3.Distance(obj.transform.position, transform.position);
Then have the script make the enemy look at your player if the distance is less than the distance you want. Ex.
var Player : GameObject; var distanceTillShoot : float;
function FixedUpdate(){ var distance = Vector3.Distance(Player.transform.position, transform.position); if (distance <= distanceTillShoot){ gameObject.transform.LookAt(Player) }
}
Then after the enemy is looking at the player have it instantiate a bullet moving forward in the local axis. Make sure to create a bullet Prefab, probably just a small sphere with a collider and rigidbody attached. Ex.
var Player : GameObject; var Bullet : Rigidbody; var projectileSpeed : float; var distanceTillShoot : float;
function FixedUpdate(){ var distance = Vector3.Distance(Player.transform.position, transform.position); if (distance <= distanceTillShoot){ gameObject.transform.LookAt(Player) var bullet : Rigidbody=Instantiate(Bullet,transform.position,Quaternion.identity); bullet.rigidbody.AddRelativeForce(transform.forward * projectileSpeed); }
}
If you want a sniper or something just increase the distance till shoot variable. ...............................................................................................................................................................................
If your just starting out start with something simpler like a game where you have to dodge
balls or something. Making a Third Person Shooter or FPS isn't for the faint of heart, it takes much
understanding and execution of scripts and components to create one.