- Home /
Setting An AI Routine
Hey, Guys!
So, I'm Learning Unity, so I'm having a hard time.
You know, I'm trying to program a simple rotine for my enemy bots, it's like this.
If the enemy doesn't spot the enemy, he'll just walk in a point a, then to the point b, just patroling, thinking about his robot life.
He'll walk to the point A, wait for 3 seconds, go to the point B, wait for more 3 seconds, back to the point A, and so on.
And if he's seeing the player, he'll pursuit him.
Anyway, i'm using NavMesh, to try to make that big boy walk around.
I just don't have a clue how to script this, could you help me?
Sorry for my english, I'm from Brazil
Answer by Griffo · Sep 07, 2012 at 07:56 PM
Put this on your Bot, create some empty gameobjects for you waypoints, however many you want, fill in the fields in the inspector, make sure you Bot has a character controller on it, then move your player towards the Bot and he, or she should attack .. Not tested ..
Hope this helps.
#pragma strict
var waypoint : Transform[]; // The amount of Waypoint you want
var patrolSpeed : float = 3; // The walking speed between Waypoints
var loop : boolean = true; // Do you want to keep repeating the Waypoints
var player : Transform; // Referance to the Player
var dampingLook = 6.0; // How slowly to turn
var pauseDuration : float = 0; // How long to pause at a Waypoint
var attackRange = 10; // Range to start the attack
var attackSpeed = 5.0; // Speed to attack
var attackLook = 10.0; // How fast to turn when attacking
private var distanceToPlayer : int; // Distance from the enemy to the Player
private var curTime : float;
private var currentWaypoint : int = 0;
private var character : CharacterController;
private var gravity : float = 2.0;
private var attacking : boolean = false;
function Awake(){
}
function Start(){
character = GetComponent(CharacterController);
}
function Update(){
distanceToPlayer = Vector3.Distance(player.position, transform.position); // Distance between the enemy and the Player
if(distanceToPlayer < attackRange){
attacking = true;
attack();
}else{
attacking = false;
}
if(currentWaypoint < waypoint.length && !attacking){
patrol();
}else{
if(loop && !attacking){
currentWaypoint=0;
}
}
}
function patrol(){
var target : Vector3 = waypoint[currentWaypoint].position;
target.y = transform.position.y; // Keep waypoint at character's height
var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude < 0.5){
if (curTime == 0)
curTime = Time.time; // Pause over the Waypoint
if ((Time.time - curTime) >= pauseDuration){
currentWaypoint++;
curTime = 0;
}
}else{
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation(target - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
}
}
function attack(){
// Attack the Player
// Rotate to face the Player
var lookPos = player.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * attackLook);
// Move towards the Player
var target : Vector3 = player.position;
var moveDirection : Vector3 = target - transform.position;
moveDirection.y = 0; // Stop the character running up off the floor to match the Players Y axis
moveDirection.y -= gravity; // Add gravity so the he always stays on the ground
character.Move(moveDirection.normalized * attackSpeed * Time.deltaTime);
}
I've tested that, is working well, but, it's not facing the target, in that case, the waypoints
Answer by RjToni · Sep 17, 2012 at 02:13 AM
Thanks for the help, It's working ok, but, I'm having problems about collisions. I've attached a Sphere collider in the bot's sword, then, if that sphere collides with the player, then the player loses a point of HP, but the collision is not working, and I Hell don't know why!!
that's the AI script so far:
var waypoint : Transform[]; // The amount of Waypoint you want
var patrolSpeed : float = 3; // The walking speed between Waypoints
var loop : boolean = true; // Do you want to keep repeating the Waypoints
var player : Transform; // Referance to the Player
var dampingLook = 6.0; // How slowly to turn
var pauseDuration : float = 0; // How long to pause at a Waypoint
var attackRange = 10; // Range to start the attack
var attackSpeed = 5.0; // Speed to attack
var attackLook = 10.0; // How fast to turn when attacking
var AtkSpeed = 1;
private var distanceToPlayer : int; // Distance from the enemy to the Player
private var curTime : float;
private var currentWaypoint : int = 0;
private var character : CharacterController;
private var gravity : float = 2.0;
private var attacking : boolean = false;
private var isattacking : boolean = false;
function Awake(){
}
function Start(){
character = GetComponent(CharacterController);
animation["attack"].speed = AtkSpeed;
animation["attack"].layer = 1;
}
function Update(){
distanceToPlayer = Vector3.Distance(player.position, transform.position); // Distance between the enemy and the Player
if(distanceToPlayer < attackRange){
attacking = true;
attack();
}else{
attacking = false;
}
if(currentWaypoint < waypoint.length && !attacking){
patrol();
}else{
if(loop && !attacking){
currentWaypoint=0;
}
}
}
function patrol(){
var target : Vector3 = waypoint[currentWaypoint].position;
target.y = transform.position.y; // Keep waypoint at character's height
var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude < 0.5){
if (curTime == 0) {
curTime = Time.time; // Pause over the Waypoint
animation.Play("idle");
}
if ((Time.time - curTime) >= pauseDuration){
currentWaypoint++;
curTime = 0;
}
}else{
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation(target - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
animation.Play("walk");
}
}
function attack(){
// Attack the Player
if (distanceToPlayer <= 1) {
animation.Play("attack");
}
else {
animation.Stop("attack");
}
// Rotate to face the Player
var lookPos = player.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * attackLook);
// Move towards the Player
var target : Vector3 = player.position;
var moveDirection : Vector3 = target - transform.position;
moveDirection.y = 0; // Stop the character running up off the floor to match the Players Y axis
moveDirection.y -= gravity; // Add gravity so the he always stays on the ground
character.Move(moveDirection.normalized * attackSpeed * Time.deltaTime);
animation.Play("run");
}
And that's is the script that I've made for the sphere collider:
var Life = 3;
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.tag=="Player") {
Life -= 1;
}
}
function Update () {
print ("Health :" +Life); }
I'm really having problems to learn about collisions and triggers =/
Your answer