I followed the Brackeys 3D movement video, and I set the CheckSphere to view collision with all layers but the player one, but it doesn’t work and doesn’t check collisions correctly.
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
CharacterController cc;
public float speed = 10f;
public float rotateSpeed = 125f;
public float jumpHeight = 5f;
public float gravity = -9.81f;
Vector3 vel;
public Transform groundCheck;
public float groundDistance = 0.48f;
public LayerMask playerMask;
bool isGrounded;
void Start()
{
cc = GetComponent<CharacterController>();
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, ~playerMask);
if (isGrounded && vel.y < 0)
vel.y = -2f;
print(isGrounded);
Move();
Jump();
vel.y += gravity * Time.deltaTime;
cc.Move(vel * Time.deltaTime);
}
void Move()
{
Vector3 direction = transform.forward * Input.GetAxis("Vertical");
cc.Move(direction * speed * Time.deltaTime);
transform.Rotate(Vector3.up * (Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime));
}
void Jump()
{
if(Input.GetAxis("Jump") > 0 && isGrounded)
{
vel.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
}
}