Search Unity

Jump

Discussion in 'Scripting' started by KiwiCops, Jun 28, 2021.

  1. KiwiCops

    KiwiCops

    Joined:
    May 26, 2021
    Posts:
    1
    My character can jump infinity but I only want them to jump one time.

    Code:
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using UnityEngine;

    public class Platformer : MonoBehaviour

    {
    // Start is called before the first frame update

    Rigidbody2D rb;
    public float moveBy;
    public float speed;
    //public float radius;
    public LayerMask layerMask;
    public bool isGrounded;
    public Transform point;
    public float jumpHeight;
    public float jumpSpeed;
    void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    }



    // Update is called once per frame
    void Update()
    {
    float x = Input.GetAxisRaw("Horizontal");
    moveBy = x * speed;
    rb.velocity = new Vector2(moveBy, rb.velocity.y);
    //sGrounded = Physics2D.OverlapCircle(point.position, radius, layerMask);
    isGrounded = true;
    // Jump code
    //f (isGrounded == true)
    if (Input.GetButtonDown("Jump"))
    {
    rb.velocity = new Vector2(rb.velocity.y, jumpHeight);
    isGrounded = false;
    }
    }

    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745