The following code powers my moving platforms. However, if there are more than 3 or so, I start to experience massive lag. As in 3 FPS lag. This seems really excessive for what I’m doing. Ideas on streamlining this? Am I doing something that really chews memory?
EDIT: I have tracked the problem down to the lines of code that scale the piston. Is scaling every frame really slow or something?
var Top : GameObject;
var Piston : Transform;
var Sound : AudioSource;
var HeightToLift : float;
var Speed : float;
var usingButton : boolean;
var linkedButton : ButtonControl;
function FixedUpdate () {
if (Top.transform.localPosition.y > HeightToLift || Top.transform.localPosition.y < 0) { // Flip direction @ ends
Speed *= -1;
}
if (usingButton == true) {
if (linkedButton.buttonPressed) {
Top.rigidbody.MovePosition(Top.rigidbody.position+(transform.up * Time.deltaTime * Speed * 3));
Piston.localScale.z += Time.deltaTime * Speed; //THIS LINE
if (Sound.isPlaying == false) {
Sound.Play();
}
} else if (Sound.isPlaying == true) {
Sound.Stop();
}
} else {
Top.rigidbody.MovePosition(Top.rigidbody.position+(transform.up * Time.deltaTime * Speed * 3));
Piston.localScale.z += Time.deltaTime * Speed; // AND THIS LINE
if (Sound.isPlaying == false) {
Sound.Play();
}
}
}