Hello! I have a working AI script that takes waypoints and if a player gets nearby it will chase the player. However I wanted it to avoid obstacles when doing those two functions. I followed an AI avoiding script using raycasts but I couldn’t get it to work with my code. I’m just not sure how I would modify the character move to use the raycast script information.
Here is my AI Script
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 ASpeed = 1;
private var distanceToPlayer : int; // Distance from the enemy to the Player
private var currentTime : 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;
private var captured : boolean = false;
function Awake(){
}
function Start(){
character = GetComponent(CharacterController);
animation["bite"].speed = ASpeed;
animation["bite"].layer = 1;
}
function Update(){
// Gets the distance between the enemy and the Player
distanceToPlayer = Vector3.Distance(player.position, transform.position);
if(captured){
animation["Death"].wrapMode = WrapMode.ClampForever;
animation.Play("Death");
}
else if(distanceToPlayer < attackRange){
attacking = true;
attack();
}else{
attacking = false;
}
if(currentWaypoint < waypoint.length && !attacking && !captured){
patrol();
}else{
if(loop && !attacking){
currentWaypoint=0;
}
}
}
function patrol(){
var target : Vector3 = waypoint[currentWaypoint].position;
target.y = transform.position.y; // This keeps the waypoint at character's height
var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude < 0.5){
if (currentTime == 0) {
currentTime = Time.time; // Pause over the Waypoint
animation.Play("idle");
}
if ((Time.time - currentTime) >= pauseDuration){
currentWaypoint++;
currentTime = 0;
}
}else{
// This gets the location of the waypoint and rotates the AI to look at the waypoint and dampen the rotation
var rotation = Quaternion.LookRotation(target - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
moveDirection.y = 0;
moveDirection.y -= gravity; //Gravity so the the AI stays on the ground
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
animation.Play("trot");
}
}
function attack(){
// Attack the Player
if (distanceToPlayer <= 2) {
animation.Play("bite");
//var ph: PlayerHealth = gameObject.GetComponent("PlayerHealth");
//var ph : PlayerHealth = (PlayerHealth)target.GetComponent("PlayerHealth");
//ph.AdjustCurrentHealth(-10);
} else {
animation.Stop("bite");
// 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; // This helps stop the AI running up off the floor to match the Players Y axis
moveDirection.y -= gravity; //Gravity so the the AI always stays on the ground
character.Move(moveDirection.normalized * attackSpeed * Time.deltaTime);
animation.Play("run");
}
}
function OnCollisionEnter(collision : Collision){
if(collision.gameObject.tag == "Projectile"){
captured = true;
}
}
Here is the AI Raycast Avoiding Script
function Update () {
//The directional Vector to our target
var dir = (target.position - transform.position).normalized;
var hit : RaycastHit;
//check for forward raycast
if(Physics.Raycast(transform.position, transform.forward, hit, 20)) {
if(hit.transform != transform) {
dir += hit.normal * 20;
}
}
var leftR = transform.position;
var rightR = transform.position;
leftR.x -=2;
rightR.x +=2;
if(Physics.Raycast(leftR, transform.forward, hit, 20)) {
if(hit.transform != transform) {
dir += hit.normal * 20;
}
}
if(Physics.Raycast(rightR, transform.forward, hit, 20)) {
if(hit.transform != transform) {
dir += hit.normal * 20;
}
}
var rot = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
transform.position += transform.forward * 20 * Time.deltaTime;
}