Hello everyone, I made a controller responsible for the movement of the character. Movement is implemented using RigidBody. But there was such a problem: when I jump and hit the wall in the air, I kind of stick to it and don’t fall to the ground, I can even walk left or right on it. Can you tell me how to fix this.
Here is the code responsible for movement:
using UnityEngine;
public class FPSInput2 : MonoBehaviour
{
public float speed = 6f;
public float jumpForce = 5f;
Rigidbody rb;
bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
void Update()
{
}
private void FixedUpdate()
{
HandleMove();
CheckGround();
HandleJump();
}
void HandleMove()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveZ = Input.GetAxisRaw("Vertical");
Vector3 moveDirection = (transform.right * moveX + transform.forward * moveZ).normalized;
Vector3 velocity = rb.linearVelocity;
if (moveX != 0 || moveZ != 0)
{
velocity.x = moveDirection.x * speed;
velocity.z = moveDirection.z * speed;
}
else
{
if (isGrounded)
{
velocity.x = Mathf.Lerp(velocity.x, 0, Time.deltaTime * speed * 0.5f);
velocity.z = Mathf.Lerp(velocity.z, 0, Time.deltaTime * speed * 0.5f);
}
}
rb.linearVelocity = velocity;
}
void HandleJump()
{
if (Input.GetAxis("Jump") > 0 && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
void CheckGround()
{
isGrounded = Physics.Raycast(transform.position, Vector3.down, 1.1f);
}
}