Disabling part of code?

Hello,

I am working on a code to control when character’s abilities are available to the player. There are many abilities and I’m using timers and variables to check wether the action can be performed. Here’s an example:

if(Input.GetKey(KeyCode.Alpha2))
		{
		
			if ((canBlast > blastDelay) && (!casting))
			//apple throw is ready to go!
			{
				ResetStates();
				casting = true;
				Character.GetComponent(Player_Sidescroll2).enabled = false;
				playerSprite.DoAnim("Special2");
				playerShadow.DoAnim("Special2");
				playerSprite.SetAnimCompleteDelegate(AnimsEnd);
				blasted = true;
				preBlast = true;
				canBlast = 0;
				g_Skill2b.guiTexture.texture = Skill2bFaded;
			}
		}

The casting variable decides whether another ability is already being used. Also the movement is controlled by the Player_Sidescroller2 script so it is disabled while casting. The problem is, this method is messy and brings out a lot of bugs when there are a lot of abilities. Is there a better way to do this, such as disabling parts of code? Or would it be better to separate each ability to different codes and disables those each time?

Thanks very much for your time.

I would prefer separating it into several classes (or functions). This makes the codes cleaner, more easy to read and maintain and less likely to have bugs.

In programming a good guideline for creating clean code (that is easy to maintain) is using the Uncle Bobs Principles of Object Oriented Design and the Single Responsibility Principle in particular.

I also strongly recommend his book Clean Code

There are a few things you could do.

First is making a funtion that holds all actions that are common between the skills, and calling this function in every skill.

Second is making a base class, that holds all common actions. Then you can derive a script/class from the base class, and add all specific actions.

As far as disbling goes, you can just comment parts out, to test what works, and what not.

Hope this helps…