- Home /
How do I stop jumping in midair?
I want to stop my player from jumping in midair but i don't know how to use a bool. Does anyone know how to stop this with my script?
My script:
{ float leftRight; float jump; public float speed; public float jumpSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Assigns inputs
leftRight = Input.GetAxis("Horizontal");
jump = Input.GetAxis("Jump");
// Jump & walk
transform.Translate(Vector3.right * Time.deltaTime * speed * leftRight);
transform.Translate(Vector3.up * Time.deltaTime * jumpSpeed * jump);
& I'm a beginner & I want to keep my script about the same.
Answer by sacredgeometry · Apr 02, 2020 at 11:16 PM
You need a way to check if the player is grounded. There are a few ways to do this one of which is ray or sphere casting and checking for the ground.
It really depends on your game which technique is most appropriate but most approaches are just evaluating if the player is grounded. and using that the clause for an if statement to allow for the jumping action i.e.
if(!IsGrounded && Input.GetButtonDown("Jump")) {
Jump();
}
What do you mean the && doesnt work?
Oh sorry yeah its not the && its the fact that you are using GetAxis. You would either need to use a button/key down or test the value I will update the answer
Answer by Gpopcorn · Apr 02, 2020 at 11:18 PM
You have to check if you are grounded like this:
if (characterController.isGrounded == true)
{
// jump
}
and to reference your character controller do this
CharacterController characterController;
void Start()
{
characterController = GetComponent<CharacterController>();
}
Hope this helps!
If your using a character controller it should be underneath your player in the inspector window. If not then I don't know how to fix your mid-air jump problem.
I don't know but its currently what I'm using for one of my games and its pretty good.
Answer by Vipertex13 · Apr 11, 2020 at 06:29 AM
Hello!
Here is my code should work
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody playerRB;
public float jumpForce = 10;
public float gravityModifier;
public bool isOnGround = true;
// Start is called before the first frame update
void Start()
{
playerRB = GetComponent<Rigidbody>();
Physics.gravity *= gravityModifier;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Space) && isOnGround)
{
playerRB.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isOnGround = false;
}
}
private void OnCollisionEnter(Collision collision)
{
isOnGround = true;
}
}
Hope it worked
.
.
.
.
PS: Make sure you add a rigid body and I am using C#
Answer by csprogrammer · Apr 11, 2020 at 08:37 AM
You need to add a ground check so something like private isGrounded;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer