I have set up a very basic character controller based first person movement script, just walking and jumping, but the jumping is not behaving as expected.
When I jump and the players head hits a ceiling, the player should fall back down, but with my current implementation, that is not happening.
Here is my script for reference, which i smostly based on an old brackeys tutorial:
public float speed = 12, jump = 12;
CharacterController characterController;
public Transform groundCheck;
public LayerMask ground;
Vector3 vel;
public float grav = -9.81f;
bool grounded;
void Start()
{
characterController = GetComponent<CharacterController>();
}
private void Update()
{
grounded = Physics.CheckSphere(groundCheck.position, 0.3f, ground);
Debug.DrawLine(groundCheck.position, groundCheck.position + -Vector3.up*0.3f);
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
Debug.Log(characterController.velocity.y);
characterController.Move(move * speed * Time.deltaTime);
if(!grounded)
{
vel.y += grav * Time.deltaTime * 2;
}
if(grounded)
{
if(Input.GetButtonDown("Jump"))
{
vel = Vector3.up * jump * 10;
}else{
if(vel.y <= 0)
{
vel = Vector3.zero;
}
}
}
characterController.Move(vel * Time.deltaTime);
}
Any advice or fixes to my code would be much appreciated.
note that the * 10 in "vel = Vector3.up * jump * 10;" is just for testing purposes, and is not actually part of the problem.
– JohnnyOlBoy