Overview:
I have a first person character controller (shown below) attached to a GameObject with a capsule colider and rigidbody.
Issue:
When the character is told to walk into a wall they will stop being affected by gravity and will simply float.
Any help would be greatly appreciated.
Current Player Controller Code: (updated after the reply made by: adi7b9)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[Header("Movement")]
[Tooltip("Walk speed in units per second.")] public float walkSpeed;
[Tooltip("Sprint speed in units per second.")] public float sprintSpeed;
[Tooltip("Air control speed in units per second.")] public float airSpeed;
[Tooltip("Jump force applied upwards.")] public float jumpForce;
[Header("Camera")]
[Tooltip("Mouse look sensitivity.")] public float lookSensitivity;
[Tooltip("Highest we can look.")] public float maxLookX;
[Tooltip("Lowest we can look.")] public float minLookX;
private float rotationX; // current x rotation of camera
private Camera cam;
[SerializeField] private Rigidbody rb;
private void Awake()
{
// get the camera
cam = Camera.main;
// disable the cursor
Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
CameraLook();
}
private void FixedUpdate()
{
Movement();
}
private void Movement()
{
//Checking if the player is on the ground
if (Grounded())
{
if (Input.GetKey(KeyCode.LeftControl))
Move(sprintSpeed);
else
Move(walkSpeed);
Jump();
}
else
{
Move(airSpeed);
}
}
private bool Grounded()
{
Ray ray = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(ray, 1.1f))
return true;
else
return false;
}
private void Move(float speed)
{
// get the keyboard inputs
float x = Input.GetAxis("Horizontal") * speed;
float z = Input.GetAxis("Vertical") * speed;
// convert global direction to local direction
Vector3 dir = transform.right * x + transform.forward * z;
dir.y = rb.velocity.y;
// apply the velocity
rb.velocity = dir;
}
private void Jump()
{
if (Input.GetButtonDown("Jump"))
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
private void CameraLook()
{
// get the mouse inputs
float y = Input.GetAxis("Mouse X") * lookSensitivity;
rotationX += Input.GetAxis("Mouse Y") * lookSensitivity;
// Set max look up and down
rotationX = Mathf.Clamp(rotationX, minLookX, maxLookX);
// rotate player and camera
cam.transform.localRotation = Quaternion.Euler(-rotationX, 0, 0);
transform.eulerAngles += Vector3.up * y;
}
}