简体   繁体   中英

Unity5 : 2D Platformer : Jumping with to different height

I'm trying to implement character jump in C# But I want him to jump to different height, depends on how log you hold a Jump button.
For example if you hit space instantaneously - you'll jump not so hight On the opposite - the character is going to jump higher, but at limited high.
Here is my code :

private Rigidbody2D rb;
private Transform lowerPoint;
private new SpriteRenderer renderer;
private Animator anim;
private Transform spawPosition;

[SerializeField]
float speed = 3f;
[SerializeField]
float jumpForce = 3f;
[SerializeField]
Image backgroundImage;
[SerializeField]
LayeredSpikes script;

float secondJumpForce;
[SerializeField]
float additionalVelocity;
const float ADDITION_VELOCITY_LIMIT = 0.9f;

bool isGrounded;
bool isAlive;
bool hasSecondJump;

void Awake()
{
    rb = GetComponent<Rigidbody2D> ();
    renderer = GetComponent<SpriteRenderer> ();
    lowerPoint = GetComponentInChildren<Transform> ();
    anim = GetComponent<Animator> ();
    spawPosition = GameObject.FindGameObjectsWithTag ("Respawn")[0].GetComponent<Transform> ();
}
void Start()
{
    ResetVelocity ();
    isGrounded = false;
    isAlive = true;
    hasSecondJump = true;
    Spawn ();
    secondJumpForce = jumpForce * 0.75f;
}

void Update()
{
    if (isAlive) 
    {
        if (isGrounded) 
        {
            AnimState = AnimationState.Idle;
            hasSecondJump = true;
            additionalVelocity = 0f;
        }

        if (Input.GetButtonDown ("Jump")) 
        {
            Jump ();
        }

        if (Input.GetButton ("Jump")
            && additionalVelocity <= ADDITION_VELOCITY_LIMIT) 
        {
            additionalVelocity += ADDITION_VELOCITY_LIMIT / 3;
            rb.AddForce( Vector2.up * additionalVelocity );
        }

        if (Input.GetButton ("Horizontal")) 
        {
            Run ();
        }
    }
}

private void Jump()
{
    AnimState = AnimationState.Jump;
    float force;

    if (isGrounded) 
    {
        force = jumpForce;
    } 
    else 
    {
        if (hasSecondJump) 
        {
            ResetVelocity ();
            force = secondJumpForce;
            hasSecondJump = false;
        } 
        else 
        {
            force = 0f;
        }
    }
    rb.AddForce (Vector2.up * force, ForceMode2D.Impulse);
    if (force != 0f) 
    {
        //script.Trigger ();
    }
}

void ResetVelocity()
{
    rb.velocity = Vector2.zero;
    additionalVelocity = 0f;
}

I'm trying to add force right in the middle of jump, but no result.

At a first glance, I can observe your bool variable isGrounded is always false . This problem was tricky, because it's your second jump that actually works, but not your first jump, that's why you were having trouble to detect it!

Set your variable isGrounded = true as a default on Start(), and then when you make your first jump, set isGrounded = false .

You can then use Colliders to detect when your character is back on ground, and if so, you will need to make your isGrounded variable public isGrounded .

Hope I was able to help!

  • Noe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM