- Home /
What is the best way to avoid AI collision with my script?
I'm trying to make an AI script that performs zombie-like actions, but I'm running into a few problems. The first problem I had was that when I used transform.position = Vector3.MoveTowards(); to move the AI directly to the player (since I do not want the zombies to roam around, I want them to come directly towards the player),the two AI that I have run into each other and it seems as if they are transparent. I tried to fix this with a code from a tutorial I found by using way points. However, when I use the way points, the AI just stays in place and never moves. So, with the code below, what would be the best approach to this problem? I would like to learn how to code an AI, so please don't recommend programs to me. What I would like the AI to do is follow the player and move around obstacles in order to get to the player.
@script RequireComponent(CharacterController)
public var wayPoints : Waypoints;
//Animations//
public var _animation : Animation;
public var runAnimation : AnimationClip;
public var runAnimationSpeed : float = 2.0;
public var idleAnimation : AnimationClip;
public var idleAnimationSpeed : float = 1.0;
//Get player... might be an array in the future.//
public var player : Transform;
public var AI : Transform;
//Ragdoll//
public var death : GameObject;
//Movement Speeds//
public var rSpeed : int = 9;
public var wSpeed : int = 2;
//Vectors//
public var v3_targetDirection : Vector3;
public var v3_moveDirection : Vector3;
//Get Distance from character to player//
public var distanceAway : float;
//More Speeds//
private var f_verticalSpeed : float = 0.0;
private var f_moveSpeed : float = 0.0;
//Health//
public var aiHP : float = 100;
private var aiMaxHP : float;
//Character Controller//
private var controller : CharacterController;
//Collison Flag//
private var c_collisionFlag : CollisionFlags;
//Movement Speeds//
private var f_movementSpeed : float = 0.0;
//Booleans//
private var b_isStop : boolean;
private var b_isAlive : boolean;
//rotate//
private var f_rotateSpeed : float = 1.2; //Smooth speed of rotation
//weapon//
var weapon : Rigidbody;
var throwSpeed : float;
function Awake() {
controller = GetComponent(CharacterController);
b_isAiming = false;
b_isStop = false;
b_isAlive = true;
c_collisionFlag = CollisionFlags.CollidedBelow;
aiMaxHP = aiHP;
_animation[runAnimation.name].speed = runAnimationSpeed;
_animation[runAnimation.name].wrapMode = WrapMode.Loop;
_animation[idleAnimation.name].speed = idleAnimationSpeed;
_animation[idleAnimation.name].wrapMode = WrapMode.Loop;
}
public function GetHpPercent() : float {
return aiHP/aiMaxHP;
}
function Start () {
transform.position = wayPoints.StartPosition();
}
function Update () {
var v3_targetDirection : Vector3 = wayPoints.GetDirectionToPlayer(transform, player);
GetHpPercent();
_animation.CrossFade(runAnimation.name);
if(aiHP <= 0){
b_isAlive = false;
}
if(b_isAlive == true){
distanceAway = GetDistance(AI, player);
}
if(distanceAway <= 5){
_animation.CrossFade(idleAnimation.name);
/*
var newProjectile : Rigidbody = Instantiate(weapon, AI.position, AI.rotation);
newProjectile.rigidbody.velocity = transform.forward * throwSpeed;
*/
rSpeed = 0;
}else{
rSpeed = 9;
_animation.CrossFade(runAnimation.name);
}
if(v3_targetDirection != Vector3.zero){
v3_moveDirection = Vector3.Slerp(v3_moveDirection, v3_targetDirection, f_rotateSpeed * Time.deltaTime);
}
var v3_movement : Vector3 = (v3_moveDirection * f_moveSpeed) + Vector3 (0, f_verticalSpeed, 0); // Apply the vertical speed if character fall down
v3_movement *= Time.deltaTime;
controller.Move(v3_movement);
if(v3_moveDirection != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(v3_moveDirection);
Debug.Log("You're moving!");
}
}
function GetDistance (_AI : Transform, _player: Transform) {
var distance : float = Vector3.Distance(_AI.transform.position, _player.transform.position);
return distance;
}
function OnTriggerEnter(col : Collider){
if(col.tag == "EnergySaw"){
aiHP -= 20.0;
if(aiHP <= 0){
aiHP = 0;
var ragdollPrefab : GameObject = Instantiate(death, transform.position, transform.rotation);
}
}
}
public function OnDrawGizmos() : void {
if (player != null) {
Gizmos.color = Color.blue;
Gizmos.DrawLine(transform.position, player.position);
}
}
It's a lot of code, sorry about that. Thank you in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220613083854im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Telling AI not to push each other around? 1 Answer
Avoidance Behaviour 2 Answers
without using NavMesh, How to avoid autonomous moving agents/obstacles 1 Answer
Resize Array Based on Value 2 Answers
A.I. Avoid Light 1 Answer