- Home /
Can't fix slow jumping for character controller 3D
Hi gameDevs,
Trying to make my Player jump with the normal speed. For now it happens pretty slowly.
I'm using CharacterController and dimensions (scale) seem to be fine.
Here's the code (based on Brackeys tutorials):
public class movementControlls: MonoBehaviour
{
public CharacterController controller;
public Transform cam;
private float VerticalSpeed;
public float speed = 30f;
public float gravity = -9.8f;
public float jumpHeight = 10f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if(direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref
turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
}
Would appreciate any help. It's my first game and I keep applying various solutions but nothing seems to work.
Thanks in advance!
It can be anything really, character controllers do not depend on scale as much as rigidbodies, i had to keep the gravity at 40 for a smooth fall....try tweaking the values until you find what you like
Good to know! I will go on tweaking then, thanks.
Answer by maelachy · Aug 10, 2021 at 01:06 PM
So my assumption was that I'm not supposed to mess with the Player's gravity cause it's meant to work with the default -9.8 value. I thought I made a mistake in the code or need to add some force to make it right. I was wrong, gravity = -40 works perfectly Thanks @rage_co