Reputation: 7
I've been trying to implement shapeshifting mechanic to my game. And there are three current modes that you can switch between. But there seems to be a problem with the if checks im using or the way im using it. I asked some people but they told me to use state machines which i found really complicated for my simple code. But i would also like to hear your opinion.
Here's the code:
if (Input.GetKeyDown(KeyCode.Space))
{
if (normalMode == true || normalMode == false && speedMode == true && (heavyMode == false))
{
normalMode = false;
heavyMode = true;
sr.sprite = heavyForm;
speed = 7f;
rb.mass = 2f;
}
else if (normalMode == true || normalMode == false && (heavyMode == true && (speedForm == false)))
{
normalMode = false;
speedMode = true;
sr.sprite = speedForm;
speed = 15f;
rb.mass = 0.5f;
}
else
{
normalMode = true;
sr.sprite = normalForm;
speed = 10f;
rb.mass = 1f;
}
}
only the if and else statements work and the else if one doesn't. Would be glad if someone helped me out.
Upvotes: -1
Views: 96
Reputation: 77354
Okay, much to unpack here.
You have three seperate states. Normal. Speed. Heavy. And it seems it can be exactly one of those. But you have a mess of a variables that you need to be extra careful with, to not accidentially set them to a state where it's all broken. Because right now, you could set them to a state, where they are all of the above. Or two. Or none. Broken. Don't build a program where "broken state" is possible. That makes finding mistakes much easier, because a big part can be caught be your compiler.
Then, don't mix two concerns. One concern is setting values for each mode. One concern is changing modes. Seperate them out. This way it is easier to see what happens.
So we write a function for setting values, to make code more readable, and we use an Enum to create our own datatype, which can be only one of the states, either Normal, or Speed, or Heavy. There is no way to accidentially screw up the multiple state variables, if there is only exactly one:
enum CharacterMode { Normal, Speed, Heavy }
// somewhere where you keep your variables:
var currentMode = CharacterMode.Normal;
if (Input.GetKeyDown(KeyCode.Space))
{
if (currentMode == CharacterMode.Normal)
{
SwitchToMode(CharacterMode.Heavy);
}
else if (currentMode == CharacterMode.Heavy)
{
SwitchToMode(CharacterMode.Speed);
}
else if (currentMode == CharacterMode.Speed)
{
SwitchToMode(CharacterMode.Normal);
}
}
// somewhere where you keep your functional logic:
public void SwitchToMode(CharacterMode mode)
{
this.currentMode = mode;
switch(mode)
{
case CharacterMode.Heavy:
sr.sprite = heavyForm;
speed = 7f;
rb.mass = 2f;
break;
case CharacterMode.Speed:
sr.sprite = speedForm;
speed = 15f;
rb.mass = 0.5f;
break;
case CharacterMode.Normal:
sr.sprite = normalForm;
speed = 10f;
rb.mass = 1f;
break;
}
}
(I don't have a compiler at hand, sorry if I missed a semicolon or bracket somewhere)
Upvotes: 3
Reputation: 37460
The else if
will never be satisfied due to this part of condition
normalMode == true && normalMode == false
which never be true and whole expression will evaluate to false
.
You have to correct that in some way.
Upvotes: 0