x


Hold Spacebar, Stop Jumping

Hello,

My character jumps using spacebar. When I HOLD spacebar down, he jumps over and over and over. I want him to perform the jump each time spacebar is pressed; but not when spacebar is held down. The script I'm using is below:

using UnityEngine;

using System.Collections;

public class Walker : MonoBehaviour {

private IRagePixel ragePixel;

public enum WalkingState {Standing=0, WalkRight, WalkLeft, JumpUp};
public WalkingState state = WalkingState.Standing;
public RagePixelSprite space;
public float walkingSpeed = 10f;


void Start () {
    ragePixel = GetComponent<RagePixelSprite>();
}

void Update () {
    if (Input.GetKey("space"))
    {
        state = WalkingState.JumpUp;
    }
    else if (Input.GetKey(KeyCode.LeftArrow))
    {
        state = WalkingState.WalkLeft;
    }
    else if (Input.GetKey(KeyCode.RightArrow))
    {
        state = WalkingState.WalkRight;
    }
    else
    {
        state = WalkingState.Standing;
    }

    switch (state)
    {
         case (WalkingState.JumpUp):
            ragePixel.SetHorizontalFlip(false);
            ragePixel.PlayNamedAnimation("JUMP", false);
            break;

        case(WalkingState.Standing):
            ragePixel.SetHorizontalFlip(false);
            ragePixel.PlayNamedAnimation("STAY", false);
            break;

        case (WalkingState.WalkLeft):
            ragePixel.SetHorizontalFlip(false);
            ragePixel.PlayNamedAnimation("WALK", false);
            break;

        case (WalkingState.WalkRight):
            ragePixel.SetHorizontalFlip(false);
            ragePixel.PlayNamedAnimation("WALK", false);
            break;

    }
}

}

Any help is greatly appreciated. Thank you so much!

more ▼

asked Jun 21 '12 at 07:35 PM

shawnkilian gravatar image

shawnkilian
361 21 28 41

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

You can use the Input.GetKeyDown and Input.GetKeyUp. Set a bool to true when it's down, and when GetKeyUp is captured and the bool is true, jump (and set the bool back to false).

more ▼

answered Jun 21 '12 at 07:43 PM

Drakestar gravatar image

Drakestar
916 6 7 14

Thank you for the help. I'm having problems adding a bool because it never recognizes the variable I set. I tried using public, private, etc but none of them worked. do I need to label it differently?

Jun 21 '12 at 07:56 PM shawnkilian

A private field (= a variable that belongs to the class, declared outside any methods) should always retain its state between function calls.

Jun 21 '12 at 08:00 PM Drakestar
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x786
x334
x212
x8
x4

asked: Jun 21 '12 at 07:35 PM

Seen: 570 times

Last Updated: Jun 21 '12 at 08:00 PM