When are Voids excuted?

Hi everyone, i am currently trying to create a simple jump player.

So far i can make the player move and jump and detect when its on the ground.
I have hooked this up to a sprite animation, so you get a jump animation when jumping, a walk animation when walking etc.

One issue i am finding is if you jump and move at the same time, the animation will not reset to idle until you let go of the arrow key. This will leave the player on the floor in the jump animation.

my code is as follows :

if (jumping == true) {


        hit = Physics2D.Raycast(transform.position,Vector2.down);  
       Debug.Log (hit.distance );
       if (hit.distance < 0.008f){
         jumping = false;

       }

     }   
     Debug.Log (jumping);
     Debug.Log (moveHorizontal);

     Animating (moveHorizontal);
   }

void Animating(float moveHorizontal)
   {
     //Debug.Log (moveHorizontal);
   if (moveHorizontal != 0f && jumping == false ) {
       anim.SetInteger ("status", 1); // Plays walking animation
     }

     if (moveHorizontal == 0f ) {
       anim.SetInteger ("status", 0); // Plays idle animation
     }

     if (jumping == true) {
       anim.SetInteger ("status", 2); // Plays jumping animation
     }
   }

Is my issue down to the animating void will not kick in until the other action of me holding move is complete hence why the animation status wont change.
using debug, all the criteria of movement not = 0 and jumping = false are in place but the staus wont change.

Any help would be cool.

Thanks

And one min later i figure it out. :slight_smile:

I hadn’t set a transition in the animation controller to go from jumping to walking.

You should say “method” instead of “void”.

A method can optionally return a result. When you don’t want it to return anything you put the word “void”. That’s what the void means: no result.

Just FYI. Glad you found the problem. :slight_smile:

Oh I see, im coming off the back of only ever coding in vba. So I’ve been seeing voids like subs.

So am I right in thinking if I had a like in the update void where I wanted to check say a players health as I want this information from a separate routing I would you method as I want information back from it?

So me using void for my animating controllers is not the correct method?

Yeah, I see where you’re coming from and you’re on the right track. A void method in C# is exactly like a sub in VB but the syntax is different. In C# every method (whether or not it returns a value) has this format:

()

There is no word like “sub” or “function” in C# syntax. You use the return type to define whether the method returns a value (like a VB function) or not (like a VB sub).

If you want to create a method to get the player’s health you could write something like:

int GetHealth()
{
   return health;
}

It’s always a good idea to break up your code into small methods.

1 Like