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?