Hello UA people.
My frame rate isn’t particularly low at the moment, but i am exporting to flash so want to optimize it as much as i possibly can.
Profiler is indicating that the below script is at 39.8% not sure exactly what these figures mean, but it surely can’t be good.
Can anyone recommend alterations to this script that would be more efficient? It should be pretty straight forward to you guys, but if anything is unclear i’ll happily explain.
I realise there is a lot in the update, but as far as i can figure there isn’t any other way, but i’m sure i’m wrong! haha.
Cheers guys!
#pragma strict
var playerInRange : boolean = false;
var waypoints : Transform [];
var currentPoint : int = 0;
var state : String = "patrol";
var speed : float = 3;
var gravity : float = 15;
private var controller : CharacterController;
private var player : Transform;
var shooting : boolean = false;
var bulletFab : Rigidbody;
var gunTip : Transform;
var power : float = 1000;
var shotDelay : float = 1;
private var bullet : Transform;
function Start(){
player = gameObject.FindWithTag("Player").transform;
controller = GetComponent(CharacterController);
}
function OnTriggerEnter (hitPlayer : Collider) {
if(hitPlayer.gameObject.tag == "Player") {
Debug.Log("collision detected");
playerInRange = true;
}
}
function Update () {
if(player) {
if (playerInRange == true) {
state = "chase";
Debug.Log("state is chase");
}else{
state = "patrol";
Debug.Log("state is patrol");
}
if(state == "patrol"){
if(currentPoint < waypoints.length) {
Mover(waypoints[currentPoint].position);
}
} else if (state == "chase"){
if(!shooting) shootStuff();
Mover(player.position);
}
}
}
function Mover(target : Vector3){
var diffVector: Vector3 = target - transform.position;
var movement : Vector3;
if(diffVector.magnitude > 1){
movement = (diffVector.normalized * speed);//without this it will run toward something and slow down when it gets close.
}else{
currentPoint = (currentPoint + 1) % waypoints.length;
}
movement.y -= gravity * Time.deltaTime;
controller.Move(movement * Time.deltaTime);
if(state == "patrol") {
transform.LookAt(target);
} else if (state =="chase"){
transform.LookAt(Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
}
// look at waypoints, works because waypoints are the same height as the player.
// then if it's chase look at the player but only on the y axis
// also the other axis are frozen.
}
function shootStuff(){
shooting = true;
var fwd = transform.TransformDirection(Vector3.forward);
var bulletShot : Rigidbody = Instantiate(bulletFab, gunTip.position, gunTip.rotation);
bulletShot.AddForce(fwd);
yield WaitForSeconds(shotDelay);
shooting = false;
if(gameObject.FindWithTag("bullet")) {
yield WaitForSeconds(4);
Destroy(gameObject.FindWithTag("bullet"));
}
}