Here's what i've got so far, as it says in the title; I want my AI to move around obstacles when he is chasing the player, if anyone would be kind enough to give me some tips it would be much appreciated.
so far, the enemy chases the player when they get into range and if the enemy loses sight of the player, it will go back the other way and patrol using raycast...
var speed : float = 9;
var rotateSpeed : float = 1;
private var direction : float;
var forwardRay : float = 2;
var leftRay : float = 5;
var rightRay : float = 5;
var player : Transform;
var MaxDistance : int = 30;
var MinDistance : int = 5;
function Update () {
var distance : Vector3 = player.position - transform.position;
//if player is in range, enemy will follow
if(distance.magnitude < MaxDistance){
if(distance.magnitude > MinDistance){
Debug.DrawLine(transform.position, player.position);
if(!Physics.Linecast(transform.position, player.position)){
Pursue();
}else if(Physics.Linecast(transform.position, player.position)){
Patrol();
}
}
}else{
Patrol();
}
}
function Patrol(){
//if there is nothing within range, then move forward
if(!Physics.Raycast(transform.position, transform.forward, forwardRay)){
transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
}else{
//if there is something to the left, then direction = 1
if(Physics.Raycast(transform.position, -transform.right, leftRay)){
direction = 1;
//if there is something to the right, the direction = -1
}else if(Physics.Raycast(transform.position, transform.right, rightRay)){
direction = -1;
//if there is nothing to the left or right, then direction = 1
}else{
var RandomDirection = Random.value;
if(RandomDirection > 0.5){
direction = 1;
}else{
direction = -1;
}
}
//rotate 90 degrees in the direction (1 = right, -1 = left)
transform.Rotate(Vector3.up, 90 * rotateSpeed * direction);
}
}
function Pursue(){
var distance : Vector3 = player.position - transform.position;
//move towards player
velocity = distance.normalized * speed;
rigidbody.velocity = velocity;
}