Hello I am trying to make 3D game but I have a problem with jumping near colliders.
If I jump next to collider it works but as soon as I start walking towards the block with collider and jump, it starts glitching and don’t want to jump
I’m not sure what is the issue, maybe something with my groundCheck object
Here’s the code:
using UnityEngine;
public class Movement : MonoBehaviour
{
public CharacterController Player;
public float speed = 12f;
public float sprintSpeed = 24f;
public float jumpHeight = 0.5f;
public Transform groundCheck;
public float groundDistance = 0.7f;
public LayerMask groundMask;
public float gravity = -9.81f;
public Camera myCam;
Vector3 velocity;
bool isGrounded;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float z = Input.GetAxis("Vertical");
float x = Input.GetAxis("Horizontal");
Vector3 move = transform.right * x + transform.forward * z;
if(Input.GetKey("left shift"))
{
Player.Move(move * sprintSpeed * Time.deltaTime);
myCam.fieldOfView += 0.2f;
if(myCam.fieldOfView >= 70)
{
myCam.fieldOfView = 70;
}
}
else
{
Player.Move(move * speed * Time.deltaTime);
myCam.fieldOfView -= 0.2f;
if (myCam.fieldOfView <= 60)
{
myCam.fieldOfView = 60;
}
}
if(isGrounded && Input.GetButton("Jump"))
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
velocity.y += gravity * Time.deltaTime;
Player.Move(velocity * Time.deltaTime);
}
}
Thank you for any advice