simple jump script

hey yall

i need a simple script where you press the space bar to jump.

Thats all!

  • Colin
if(Input.GetKeyDown("Space"))
{
    rigidBody.AddForce(0, forceToBeApplied, 0);
}
2 Likes

:o

Update() code:

        if (Input.GetKeyDown(KeyCode.Space)  isJumping == false  isFalling == false)
        {
            isJumping = true;
            viewScript.lockY = true;
            Jump();
        }

        if (isJumping == true  isFalling == false)
        {
            Jump();
        }

        if (isFalling == true  isJumping == false)
        {
            Fall();
        }

functions

public void Jump()
    {

        if (currJump < allowedJump)
        {
            float temp = 0.0f;
            temp = Mathf.Sin(Time.deltaTime) * jumpSpeed;
            currJump += temp;
            controller.Move(Vector3.up * temp * Time.deltaTime * jumpSpeed);
        }
        else
        {
            isJumping = false;
            isFalling = true;
        }
    }

    public void Fall()
    {
        if (isGrounded == false)
        {
            float temp = 0.0f;
            temp = Mathf.Sin(Time.deltaTime) * jumpSpeed;
            currJump -= temp;
            controller.Move(Vector3.up * temp * Time.deltaTime * jumpSpeed * -1);
        }
        else
        {
            isFalling = false;
            viewScript.lockY = false;
            currJump = 0;
        }
    }
1 Like

ty

can you give the variables for this

i cant wtf

1 Like

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

public class NewBehaviourScript : MonoBehaviour
{
public float speed;
public float jumpforce;
private float moveinput;
private bool isgrounded;
public Transform groundcheck;
public float checkradius;
public LayerMask whatisground;
private int extrajumps;
public int extrajumpsvalue;

private Rigidbody2D rb;

void Start()
{
extrajumps = extrajumpsvalue;
rb = GetComponent();
}

void FixedUpdate()
{
isgrounded = Physics2D.OverlapCircle(groundcheck.position, checkradius, whatisground);

moveinput = Input.GetAxisRaw(“Horizontal”);
rb.velocity = new Vector2(moveinput * speed, rb.velocity.y);
}

void Update()
{
if(isgrounded == true)
{
extrajumps = extrajumpsvalue;
}

if(Input.GetKeyDown(KeyCode.UpArrow) && extrajumps > 0)
{
rb.velocity = Vector2.up * jumpforce;
extrajumps–;
}else if(Input.GetKeyDown(KeyCode.UpArrow) && extrajumps == 0 && isgrounded == true)
{
rb.velocity = Vector2.up * jumpforce;
}
}
}

1 Like

what is a groundcheck

Groundcheck would be the ground that you are jumping from. Eg. If using a terrain, use that transfom as the position, it will be checking the Y (up) position.

It doesent work

This post is NINE YEARS OLD!!! Please don’t reply to old dead necro posts. Just make your own post. It’s free.

When you do so, keep this in mind:

How to report problems productively in the Unity3D forums:

http://plbm.com/?p=220

Help us to help you.

2 Likes