- Home /
Question by
Unity_GameBreaker · Feb 06, 2021 at 02:59 AM ·
c#movementjumping
cant jump while still
Recently, I had an issue where I could jump forever in the air. After fixing that, I can't jump while standing still. I've looked through some forum posts but only came across one, which didnt solve anything. Any Ideas on how to fix this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
if(controller.isGrounded == true && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if(Input.GetButtonDown("Jump") && controller.isGrounded == true)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Comment
Answer by yuvaansh · Feb 06, 2021 at 04:43 AM
@Unity_GameBreaker
I think you have done a mistake. That is in your script (which is in your question) you have never set the "isGrounded" variable to true, and in your script if isGrounded is true then only you will be able to jump.
I hope this would answer your question ;-D
Your answer
![](https://koobas.hobune.stream/wayback/20220613024647im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Player Jumps instead of moving forward 0 Answers
Multiple Cars not working 1 Answer