S
S
Shikita2020-03-21 16:18:13
C++ / C#
Shikita, 2020-03-21 16:18:13

How to make a permanent jump when holding down a key?

Unity version 2019.3.6f
For example, I want to make it so that when holding down the Space key, every time it touches the ground, it jumps again.
Here is the script for all player movements:

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

public class Player_Control : MonoBehaviour
{
    public float speed = 20f;
    private Rigidbody2D rb;
    private bool FaceRight = true;
    public float JumpForse;
    private bool is_grounded;
    public Transform ground_check;
    public float check_radius;
    public LayerMask what_is_ground;
    private int extra_jumps;
    public int extra_jump_value;
    private float move_input;
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        extra_jumps = extra_jump_value;
    }
    private void Update()
    {
        if(is_grounded == true)
        {
            extra_jumps = extra_jump_value;
        }
        if (Input.GetKeyDown(KeyCode.Space) && extra_jumps > 0)
        {
            rb.velocity = Vector2.up * JumpForse;
            extra_jumps--;
        }
        else if (Input.GetKeyDown(KeyCode.Space) && extra_jumps == 0 && is_grounded == true)
        {
            rb.velocity = Vector2.up * JumpForse;
        }
    }
    void flip()
    {
        FaceRight = !FaceRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }
    private void FixedUpdate()
    {
        is_grounded = Physics2D.OverlapCircle(ground_check.position, check_radius, what_is_ground);

        if (FaceRight == false && move_input > 0)
        {
            flip();

        } else if(FaceRight == true && move_input < 0)
        {
            flip();
        }
        move_input = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(move_input * speed, rb.velocity.y);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanielMcRon, 2020-03-21
@Shikita

get key

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question