- Home /
Help optimize code
I am looking for some help with this code, I am making the game for android so every CPU cycle counts. With 3 enemies the game runs at approx 45fps. With 7 enemies it drops to around 15 fps. Checking the profiler this script is the next highest activity after camera render. I am looking for any help to optimize this code to see if that helps with FPS. To increase performance the enemy does not have a rigidbody as the physics involved were hogging the CPU.
#pragma strict
var player : Transform;
static var mummySpeed : float = 6;
var direction: float;
var timesHit:int;
var touchingPlayer:boolean;
var immune:boolean;
var mummyTexture:Texture2D;
var mummyHitTexture:Texture2D;
var mummyDamage1:Texture2D;
var mummyDamage2:Texture2D;
var mummyRig:GameObject;
var playerMidSection: Vector3;
function Update () {
if(Vector3.Distance(player.position, transform.position) > 8){
touchingPlayer = false;
direction = Random.value;
// Cast ray to player
var hit : RaycastHit;
var rayDirection = (player.position) - transform.position;
if (Physics.Raycast (transform.position, rayDirection, hit)) {
if (hit.transform == player) {
// enemy can see the player!
transform.LookAt(player);
transform.Translate(Vector3(0,0,mummySpeed) * Time.deltaTime);
//Debug.Log("I see You");
}
else {
// there is something obstructing the view randomly wander
//Debug.Log("Im blind!!");
transform.Translate(Vector3(0,0,mummySpeed) * Time.deltaTime);
}
}
//Debug.DrawLine(transform.position, player.position, Color.green);
var bodyForwardRay = transform.TransformDirection(Vector3(0,0,2));
if(Physics.Raycast(transform.position, bodyForwardRay, hit, 2)){
if (hit.collider.gameObject.tag == "Wall" || hit.collider.gameObject.tag == "Chest" || hit.collider.gameObject.tag == "Eye" || hit.collider.gameObject.tag == "Life" || hit.collider.gameObject.tag == "Sarchoph"){
if (direction < 0.5){
transform.Rotate (0,35,0);
}
else{
transform.Rotate (0,-35,0);
}
}
}
}
else
{
touchingPlayer = true;
GlobalVariables.health = GlobalVariables.health - 0.2;
}
}
function OnMouseDown ()
{
if (touchingPlayer == true && immune == false){
MummyHit();
}
}
function MummyHit(){
mummyRig.renderer.material.mainTexture = mummyHitTexture;
immune = true;
timesHit++;
yield WaitForSeconds(0.5);
immune = false;
if (timesHit == 1){
mummyRig.renderer.material.mainTexture = mummyDamage1;
}
if (timesHit == 2){
mummyRig.renderer.material.mainTexture = mummyDamage2;
}
if (timesHit == 3){
Destroy(gameObject);
}
}
You Should probably usee a Linecast and not a raycast, as a raycast might be more expensive. It doesn't allow you to tell what your colliding with, only if you are colliding with something.
Found this, and gave it a test, Linecast was slower. Not sure why as it gives less info but it dropped to 10fps with that change.
http://answers.unity3d.com/questions/164770/raycast-and-linecast.html
Answer by Avaista · Nov 28, 2012 at 04:46 AM
Also look in Collider.Raycast, Collider.Raycast
I'm not 100% sure, but I believe this is much more efficient, as it only deals with a single collider.
As for the second one, look into layer masks, and using layers instead of tags LayerMask.NameToLayer
int layermask = 1 << LayerMask.NameToLayer("Wall");
if(Physics.Raycast(transform.position, transform.forward, hit, 2, layermask))
{...}
More Info On Layers, which are mad useful, can be found here: Layers
EDIT Oh didn't notice before but you are using Renderer.material as well, you probably want to use Renderer.sharedMaterial.
Renderer.material creates a clone of the material, then returns it, so each time you use it, you are cloning a material.
You should clone the material at the beginning of the class, so that all mummies have their own material,
public void Awake() { renderer.material; }
Thanks for the advice, I am currently using the walls on a layer as I have a $$anonymous$$inimap which when items are purchased this allows the player to see the walls. This is done by changing the layer of the walls from one the $$anonymous$$imap cam doesnt see to one it does. Would that not interfere with the collisions if I make it collide with layer?
Sorry if its a silly question, what started out as a simple project has grown and I am a designer who is coding way out of my depth