Can this script be optimized?

Hey there, I’m trying to make my game run smoother, and I have reason to believe that the elevators in my game are causing a good amount of lag.

If you can find any obvious problems or ways to improve the speed of this script, it’d be greatly appreciated!

This script is attached to the actual elevator platform. It has a trigger to detect when the player gets on. When the player is onboard, you can press W or D to move up and down the elevator shaft.

#pragma strict

var player : GameObject;

player = GameObject.Find("3rd Person Controller");

var startposition : float;

var distance = 49.45;

var top = false;
var bottom = false;
var within = false;

var hitbase = false;

var onboard = false;
function Start() {

distance = 49.45;

startposition = transform.position.y;

}


//the update function checks where the player is, and if it is within the elevator's range it will move to the same level. 

function Update () {

		Debug.Log(distance);
		if(player.transform.position.y < startposition-distance)
			{
			top = true;
			within = false;
			}
		if(player.transform.position.y > (startposition+.45) )
			{
			bottom = true;
			within = false;
			}

		if(player.transform.position.y >= startposition-distance  player.transform.position.y <= startposition + .45)
			{
			top = false;
			bottom = false;
			within = true;
				if(transform.parent.gameObject.transform.position.y < player.transform.position.y - .43  !onboard)
					{
						transform.parent.gameObject.transform.Translate(Vector3.up * (Time.deltaTime * 6.5));
					}
				if(transform.parent.gameObject.transform.position.y > player.transform.position.y - .43  !onboard)
					{
						transform.parent.gameObject.transform.Translate(Vector3.down * (Time.deltaTime * 6.5));
					}
				if(onboard)
					{
						if(!LangmanController.notjumping)
						{
						transform.parent.gameObject.transform.position.y = player.transform.position.y - .43;
						}
					}
			}


}


//this is where the actual movement comes in v v v

function OnTriggerStay (col : Collider) {
	if (col.name == "Sprite") {	
		if(within || top)
			{
    		if (Input.GetButton ("W"))
 				{
				player.transform.Translate(Vector3.up * (Time.deltaTime * 7));
				transform.parent.gameObject.transform.Translate(Vector3.up * (Time.deltaTime * 7));
 				} 
 			}
 		if(within || bottom)
			{
 			if (Input.GetButton ("S"))
 				{
 				player.transform.Translate(Vector3.down * (Time.deltaTime * 7));
 				transform.parent.gameObject.transform.Translate(Vector3.down * (Time.deltaTime * 7));
 				} 
 			}

			
	}	
}
		
function OnTriggerEnter (col : Collider) {
	if (col.name == "Sprite") 
			{
				onboard = true;
				Mining.elevator = true;
			}
			

//this is just to make sure the elevator cant go farther than a certain block if the character doesnt have the required tools
	if(col.name == "Barrier1(Clone)"  Mining.drill <1)
			{
			distance = (startposition - transform.position.y);
			}
	if(col.name == "Barrier2(Clone)"  Mining.drill <2)
			{
			distance = (startposition - transform.position.y);
			}
		
}

function OnTriggerExit (col : Collider) {
	if (col.name == "Sprite") 
			{
				onboard = false;
				Mining.elevator = false;
			}
}

The elevators in this game are placeable, meaning they can be placed at different positions in the world.

Is Translate a good way to move the platform? Am I missing something? Is there a better way to make the platform stop at the top and bottom of the shaft? :face_with_spiral_eyes:

There’s not much to be optimized, but

  1. Remove the Debug.Log from Update (probably your biggest optimize here)
  2. Cache your transforms (everything helps)
  3. If you can re-work your logic to not rely on OnTriggerStay, you will be better off
  4. If you are using rigidbodies, you need to get away from using Translate/Rotate

So what should I use instead of Translate?

EDIT: Just realized I’m not using Rigidbodies in any of the elevator/player components, should I stick with translate then?

Yeah just keep Translate if youre not using rigidbodies.

If you’re moving these elevators and they have colliders it will cause a huge drop in performance if they dont have rigidbodies with isKinematic ticked on, and gravity ticked off. This signals to physx that the object is dynamic and it shouldn’t perform the costly rebuild of it’s lookup tables for these objects.

indeed.

Read this for clarification…

http://docs.unity3d.com/Documentation/Manual/Physics.html