Let’s Jump – A straightforward Unity C# script to make a Jump Button



You have a capsule as your player placeholder, and you already added a rigid body to it. Then you need to make a jump button. What would you do?

Here using this simple script you can make a jump button for your character easily.

Attach this script to the capsule with rigid body in it, then make a button on UI Canvas, then drag and drop the capsule to the onclick slot, find startLetsJump() and that’s all.

Here is the script:

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

public class LetsJump : MonoBehaviour
{
	
	public float jumpSpeed = 3f;
	public float jumpDelay = 2f;
	
	private bool canjump;
	private bool isjumping;
    private Rigidbody rb;
	private float countDown;
	
    // Start is called before the first frame update
    void Start()
    {
        canjump = true;
		rb = GetComponent<Rigidbody>();
		countDown = jumpDelay;
    }

    // Update is called once per frame
    void Update()
    {
		if(isjumping &amp;&amp; countDown > 0)
			countDown -= Time.deltaTime;
		else{
			canjump = true;
			isjumping = false;
			countDown = jumpDelay;
		}
			
    }
	
	public void StartLetsJump(){
		if(canjump){
			canjump = false;
			isjumping = true;
			rb.AddForce(0, jumpSpeed, 0, ForceMode.Impulse);
		}
	}
	
}

This script along a demonstration scene is included in a Unity package that you can download here: https://creativeshop.ciihuy.com/product/lets-jump-a-simple-way-to-jump-in-unity/

Easily download the package and import it to your Unity editor.

loading...

Leave a Reply

Your email address will not be published. Required fields are marked *