M
M
Max Bogachov2017-03-11 09:01:55
2D
Max Bogachov, 2017-03-11 09:01:55

How to block double jump in Unity?

Guys, I made a script for moving an object, and for jumping, but here the problem is you can do a double jump, here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class moving : MonoBehaviour {
  private float speed = 5f;
  private float jumpForce = 15F;
  private Rigidbody2D rigidbody;
  private SpriteRenderer sprite;

  private void Awake(){
    rigidbody = GetComponent<Rigidbody2D> ();
    sprite = GetComponent <SpriteRenderer> ();
  }

  private void Run(){
    Vector3 direction = transform.right * Input.GetAxis("Horizontal");
    transform.position = Vector3.MoveTowards (transform.position, transform.position + direction, speed * Time.deltaTime);
    sprite.flipX = direction.x < 0.0f;
  }

  private void Jump(){
    rigidbody.AddForce (transform.up * jumpForce, ForceMode2D.Impulse);
  }
  private void Update () {
    if (Input.GetButton ("Horizontal"))
      Run ();
    if (Input.GetButtonDown ("Jump"))
      Jump();
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TheTalion, 2017-03-11
@Geekinder

Add a check to see if the character is on a solid object. If there is such an object under the character, then he can jump, if not - no.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question