Hi there, I am workin on a game where you control a character over a rotating ball. It just works fine, but I am wondering if this code can be optimized. I am using coroutines and update and fixed update. Is there some advice the code could be written in a better and performant way?
// Animation State equals: 1 = Run, 2 = Jump, 3 = Slide, 4 = Surf, 5 = Crash
function Update()
{
if ( Input.GetButtonDown("Jump") )
{
SpaceBarHit = true ;
}
if(speedUp)
{
StartCoroutine("powerUps");
}
else{
StopCoroutine("powerUps");
scorePower = 1 ;
}
if (!flyHigh)
{
if (grounded)
{
// Jump
if (canJump ( GUIJump || SpaceBarHit ) ){
fleaRigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalspeed(), 0);
emitParticle(1);
if(!GUISurf)animateState = 2 ;
audio.PlayOneShot(jumpSound) ;
canJump = false ;
grounded = false ;
GUIJump = false ;
SpaceBarHit = false ;
}
else if ( GUISurf ){
animateState = 4 ;
}
else{
//runSound();
animateState = 1 ;
if(!speedUp)Maxspeed = 50 ;
destroySurf();
}
}
}
}
function FixedUpdate ()
{
fleaTransform.position.x = Mathf.Clamp(transform.position.x, -animateClamp, animateClamp);
if ( !dead )
{
if (!flyHigh)
{
sliderVal = leftRightSlider.GetComponent(UISlider).sliderValue ;
if ( !collided ){
if ( speed < Maxspeed ){
speed += Acceleration * Time.deltaTime ;
}
else{
speed -= Acceleration * 2 * Time.deltaTime ;
}
}
else if( !invulnerable ){
speed = 0 ;
animateState = 5 ;
resetCollision();
}
controlButtons(acceleroMeter);
}
else{
if(mueckeObj != null ){
if(!isFlying)
{
animateState = 5 ;
isFlying = true ;
}
fleaRigidbody.MovePosition(GameObject.Find("FleaAttach").transform.position + Vector3(-1.5,0,1));
Maxspeed = 80 ;
if ( speed < Maxspeed ){
speed += Acceleration * 10 * Time.deltaTime ;
}
}
}
getCurrentAnimation();
//Add to the distance value in the game controller
if ( speed > 0 )GameController_JS.TotalDistance += speed * Time.deltaTime;
if ( speed > 0 )GameController_JS.TotalScore += ( speed * scorePower ) * Time.deltaTime;
world.Rotate(Vector3.up * (-speed / 100), Space.Self); //move the tunnel forward based on speed
}
else
{
endDeath();
}
}