Hello, I have watched Brackey’s tutorial on movement and he puts lines of code for gravity, and then the velocity on the play kept increasing when we are on the ground so Brackey fixes it but for some reason it doesn’t work for me and my velocity still increases when im on the ground. Brackey’s Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementCharacterController : 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()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
public class PlayerMovementCharacterController : 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()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
else // If not grounded or velocity is greater than 0, apply gravity
velocity.y += gravity * Time.deltaTime;
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 * -2f * gravity);
}
controller.Move(velocity * Time.deltaTime);
}
}
I moved the part where the gravity is applied to your velocity up a bit, then locked it off behind an “else” statement that ensures it only applies when not grounded. Please lemme know if this helped!