- Home /
Question by
Gurdan · Jul 08, 2021 at 02:59 PM ·
c#if-statements
Tips on how to handle multiple if statemens
Hi,
I'm a beginner, i want to know if there are better way to handle this kind of situations, if in the future the game will be bigger, this kind of workflow can cause some performance issues? Thanks in advance.
if (scriptMovement.IsGrounded() && scriptMovement.horizontal == 0 && !scriptMovement.isShooting)
{
ChangeAnimationState(Player_Idle);
}
else if (scriptMovement.IsGrounded() && scriptMovement.horizontal < 0 || scriptMovement.IsGrounded() && scriptMovement.horizontal > 0)
{
ChangeAnimationState(Player_Run);
}
else if (!scriptMovement.IsGrounded() && rb.velocity.y < 0 && !scriptMovement.isWallSliding)
{
ChangeAnimationState(Player_FallOff);
}
else if (scriptMovement.isJumping)
{
ChangeAnimationState(Player_Jump);
}
else if (scriptMovement.isWallSliding && scriptMovement.WallSlide)
{
ChangeAnimationState(Player_WallSliding);
}
else if (scriptMovement.isShooting == true)
{
ChangeAnimationState(Player_Shooting);
Debug.Log(1);
}
Comment
Best Answer
Answer by logicandchaos · Jul 08, 2021 at 03:16 PM
You use a finite state machine (FSM) using enums and a switch statement. enum States{ IDLE, RUN, JUMP, FALL }
States state;
switch(state){
case IDLE:
break;
case RUN:
break;
case JUMP:
break;
case FALL:
break;
default:
break;
}
kind of like that.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# the position of the object 1 Answer
If gameobject moves do this 1 Answer
Value not changing back to where its supposed to... 0 Answers