I have a script I made and if I continuously spam the jump key my character starts flying. I was wondering if there’s a way to limit the script to only be able to double jump. I’m new to scripting so I’m still getting used to it all.
Script below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JumpingScript : MonoBehaviour
{
Rigidbody rb;
public float forcePower;
void Start()
{
rb = GetComponent();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
rb.AddForce(transform.forward * forcePower);
}
Hey and welcome.
The general idea is to check whether or not the player is grounded. There are different ways to do this, one would be downward raycasts. Then you’d introduce a variable containing the number of jumps the character is supposed to be able to do, and when jumping and not being grounded, reduce that number by one. Only allow jumping while that number is above 0 and return it to its default value when grounded again.
Only take the above as a general overview, as i would recommend you to look a tutorial up. There are hundreds of tutorials on movement and jumping, or more specifically double jumping.