- Home /
Player floats in air when jumping
Hey guys, Every time when I jump with my player, he jumps, but lands a little bit above the ground. Does someone know how to fix this?
Here is the movement script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MenschMovement : MonoBehaviour {
public CharacterController controller; public Transform groundCheck; public LayerMask groundMask; public float groundDistance = 20f; public float jump = 300f; public float speed = 2500f; public float jumpveloctiy = -2500f;
private Vector3 velocity; private bool isGrounded; private float gravity = -5000f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = jumpveloctiy;
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = speed * 2;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed = 2500f;
}
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && (gameObject.transform.position.y <= 500f))
{
velocity.y = Mathf.Sqrt(jump * -200.0f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Answer by metalted · Mar 01 at 09:19 PM
Usually when this kind of stuff happens, its something to do with the IsGrounded check. For example, a simple check could be to create a raycast pointing downwards to the ground. If the raycast hits the ground, the player should stop falling. But this will also have the effect, that if the raycast is to long, the player will stop in the air, because the raycast hit will return true, before the player model is hitting the ground. Other way around, if the raycast is to short, the player will fall through the floor and then the raycast will hit, stopping the player on the knees. If you have a single point for the center of your player and the origin of your raycast, you have to make sure that your raycast is half the length of your player, so that ray end lines up with the edge of your model.
Instead of a raycast your are using a sphere cast, which is basically the same but will check in a volume. Or see it as an infinite amount of raycast pointing in any possible direction from the origin. The same idea applies. You have to make sure that the radius of your sphere is the same length as half of your player, so that the edges line up. I would give that a try.
This is more of an opinion, but are you sure SphereCheck is the way to go? If you just want to check if the ground is below you, a regular raycast will achieve the same thing. The spherecheck might get you in to situations later where you are detecting objects that are not below you but around you, giving you a false grounded condition. Although you are using a layermask so it should be fine. Food for thought.