- Home /
The question is answered, right answer was accepted
Character can jump mid air forever
my character can jump forever, even in the air. How can I fix this?
here is my code:
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;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && 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") && isGrounded){
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Answer by Unity_GameBreaker · Feb 06, 2021 at 02:01 AM
Ok, so I managed to get it working by using controller.isGrounded instead of the checkSphere. But now I cant jump while standing still :/ Here's the updated code: 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);
}
}
Answer by Llama_w_2Ls · Feb 05, 2021 at 07:51 PM
IsGrounded is never set to false. Does your groundCheck object move with the player, and is the groundDistance a suitable radius, such as 0.25f? @Unity_GameBreaker
I set the groundDistance to 0.4f, I've tried 0.25f with the same results. I also tried setting IsGrounded to false and it still jumps infinitely, though I might have set it to false in the wrong spot @Llama_w_2Ls
Did you set your layermask to ground and assigned the ground layer to your floor objects?
Answer by matthewroxby · Feb 05, 2021 at 09:45 PM
Is it possible that your own character is getting picked up by the checkSphere? You might want to use tags to see if the player's jump should be refreshed