I cant get collisions between player and walls to work correctly, player flies up and camera spazzes out during the collision
I've been making a maze game and whenever the player collides with the wall, the camera spins out and the player flies up, I've tries to fix this issue many ways however have not had any success, thanks in advance for the help. Here is the movement script and the variables used: Variables:
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] float walkSpeed = 1.7f;//controls velocity
[SerializeField] float gravity = -13.0f;//controls gravity
[SerializeField] [Range(0.0f, 0.5f)] float movementSmoother = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float mouseSmoother = 0.03f;
[SerializeField] bool cursorLock = true;
float cameraPitch = 0.0f;// keeps track of x rotation(starts looking straight ahead)
float velocityY = 0.0f;
CharacterController controller = null;
Vector2 currentDirction = Vector2.zero;
Vector2 currentDirectionVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
Movement script:
void UpdateMovement()
{
Vector2 targetDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));//using wasd for the directions w = foward a = left s = backwards d = right
targetDirection.Normalize();// used otherwise when travelling diagonally would move faster than if moving a single direction
currentDirction = Vector2.SmoothDamp(currentDirction, targetDirection, ref currentDirectionVelocity, movementSmoother);
if (controller.isGrounded)
velocityY = 0.0f;
velocityY += gravity * Time.deltaTime;
Vector3 velocity = (transform.forward * currentDirction.y + transform.right * currentDirction.x) * walkSpeed + Vector3.up * velocityY; //determines the velocity of the player
controller.Move(velocity * Time.deltaTime * 1.9f);
}
and here is the camera code if the issues are separate:
void UpdateMouseLook() // pivoting thee camera on the x axis independantly of the y axis rotation
{
Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoother);
cameraPitch -= currentMouseDelta.y * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f); //ensures the camera is in the accepted ranges
playerCamera.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}