- Home /
Zombie AI Lag when checking if it should jump
Hello, I have made a very simple Zombie AI script, but i am having trouble with jumping. The current code i have works fine, but the second i have 3+ zombies jumping, unity lags extreamly bad, is there a way to stop this? is it just because i am calculating to many collisions? this is my code:
var Player : GameObject;
var Waypoint : Vector3 = Vector3.zero;
var inMob : boolean = false;
var isLeader : boolean = false;
var rotSpeed : float = 1;
var SpottingDistance : float = 10;
var Dis : float = 100;
var motor : CharacterMotor;
private var controller : CharacterController;
var AttackPlayer : boolean = false;
function Start(){
Player = GameObject.FindWithTag("Player");
controller = GetComponent(CharacterController);
motor = GetComponent(CharacterMotor);
}
function FixedUpdate(){
if (!AttackPlayer){
if (Vector2.Distance(Vector2(transform.position.x,transform.position.z),Vector2(Waypoint.x,Waypoint.z)) < 15){
AssignWaypoint(Vector3.zero);
}else{
var WaypointDirection = Waypoint - transform.position;
//controller.SimpleMove(WaypointDirection);
motor.inputMoveDirection = Vector3.ClampMagnitude(WaypointDirection,1);//transform.rotation * WaypointDirection;
}
}else{
var PlayerDirection = Player.transform.position - transform.position;
//controller.SimpleMove(WaypointDirection);
motor.inputMoveDirection = Vector3.ClampMagnitude(PlayerDirection,1);//transform.rotation * WaypointDirection;
}
var loc : Vector3 = Waypoint;
var PlayerDirectionFixed = Player.transform.position - Vector3(0,0.5,0) - transform.position;
var Yofset : float = 1.5;
Dis = Vector3.Distance(Vector2(Player.transform.position.x,Player.transform.position.z),Vector2(transform.position.x, transform.position.z));
if (Dis < SpottingDistance){
if (Dis < 16)
Yofset = 1;
if (Dis < 5.8)
Yofset = 1;
AttackPlayer = true;// Incase the ray doesn't work
var hit : RaycastHit;
if (Physics.Raycast (transform.position + Vector3(0,Yofset,0), PlayerDirectionFixed, hit, SpottingDistance+2)){//Ignore the player
if (hit.transform.tag != "Player" && hit.transform.tag != "MainCamera" && hit.transform.tag != "IgnoreRay"){
AttackPlayer = false;
Debug.DrawRay (transform.position + Vector3(0,Yofset,0), PlayerDirectionFixed, Color.red);
}else{
AttackPlayer = true;
Debug.DrawRay (transform.position + Vector3(0,Yofset,0), PlayerDirectionFixed, Color.green);
}
}else{
AttackPlayer = true;
Debug.DrawRay (transform.position + Vector3(0,Yofset,0), PlayerDirectionFixed, Color.green);
}
}else{
AttackPlayer = false;
}
if (AttackPlayer)
loc = Player.transform.position;
// if (AttackPlayer)
// Waypoint = Player.transform.position;//to come back the 'I have better waypoints to go to' glich
var targetRotation = Quaternion.LookRotation (loc - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
transform.rotation.x = 0; transform.rotation.z = 0; // rotating it towards the target/waypoint
}
function AssignWaypoint(location : Vector3){
if (location != Vector3.zero){
Waypoint = location;
}else{
var Rnd : Vector3 = Vector3.zero;
Rnd.x = Random.Range(-300,300);
Rnd.z = Random.Range(-300,300);
Rnd.y = 0;
Waypoint = Rnd;
}
}
function OnControllerColliderHit (hit : ControllerColliderHit) {
if (hit.moveDirection != Vector3.zero && hit.moveDirection.y == 0){
//var wallHit : RaycastHit;
// if (Physics.Raycast (transform.position + transform.forward, transform.forward, wallHit, 3)){//Ignore the player
// if (wallHit.transform.gameObject == null){
//
// Debug.DrawRay (transform.position + transform.forward, transform.forward, Color.blue); //add jump here
// motor.inputJump = true;
//
// }else{
// Debug.DrawRay (transform.position + transform.forward, transform.forward, Color.magenta);
// motor.inputJump = false;
// }
//
// }
Debug.DrawRay (transform.position + transform.forward, transform.forward, Color.blue); //add jump here
motor.inputJump = true;
yield WaitForSeconds(0.5);
motor.inputJump = false;
}
}
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
Tank you in advance, Lachee
Comment
Answer by sam32x · Aug 10, 2012 at 07:34 AM
function Start () {
Invoke("Loop",0.1);
}
function Loop () {
Invoke("Loop",0.1);
//lagging code goes here
}
that's how i fix code that lags
Your answer
Follow this Question
Related Questions
Simple movement causing lag (on standalone) 1 Answer
Ai Zombie Melee Attack script. 5 Answers
Why is this script causing a lot of lag? 2 Answers
Way Point Script Change Group 0 Answers
Character Motor causes serious lag. 0 Answers