Hello, I am making a simple FPS game and I have a little problem with movement.
Whenever I am walking into wall and jump, player won’t jump and starts twitching like he wants to jump but is stopped by the wall. I tried Physics.CheckBox but it doesn’t solve my problem because it just restrict player from jumping when near the wall
Here’s my code:
public CharacterController cc;
public float speed = 100f;
public float jumpHeight;
public float gravity = -9.81f;
public Transform groundCheck, collideCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public bool isGrounded;
public bool isColliding;
public bool isAbleToJump = true;
Vector3 velocity;
public float moveSpeed;
public Camera cam;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
// isColliding = Physics.CheckBox(collideCheck.position, new Vector3(0.55f, 0.5f, 0.55f), Quaternion.identity, LayerMask.GetMask("Wall"));
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float movX = Input.GetAxisRaw("Horizontal");
float movZ = Input.GetAxisRaw("Vertical");
movX = isGrounded ? movX : movX / 3;
Vector3 move = transform.right * movX + transform.forward * movZ;
moveSpeed = Input.GetKey(KeyCode.LeftShift) ? speed * 2 : speed;
if (movX == 1 || movZ == 1)cam.fieldOfView = Input.GetKey(KeyCode.LeftShift) ? cam.fieldOfView + Time.deltaTime*25 : cam.fieldOfView - Time.deltaTime * 25;
if (cam.fieldOfView >= 80) cam.fieldOfView = 80;
if (cam.fieldOfView <= 70) cam.fieldOfView = 70;
cc.Move(move * moveSpeed * Time.deltaTime);
if (Input.GetButton("Jump") && isGrounded && isAbleToJump && !isColliding)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y = velocity.y < -100f ? -100 : velocity.y + gravity * Time.deltaTime;
cc.Move(velocity * Time.deltaTime);
}
Also if there’s any better way to make movement, I am open to changes. I tried Rigidbody movement but I got stuck when I wanted a player to jump because AddForce overwhelmed me.
Thank you for any advice