Ok so I am making a first person game when I jump and collide with the wall the character wallruns this issue occurs like this:
i am running and pressing W and jumped and the wall on the right or on the left the player wallruns along the wall i wrote a code to check for raycasthit on the left or the right and stop the player movement and make him fall but seems like it doesnt work well? like it works when i jump and press A key the player doesnt get stuck on the wall just falls anyways here is my movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody playerRb;
public float speed;
public float jumpForce;
public float gravityModifier;
public Transform playerCamera;
private Vector3 moveDirection;
private Camera playerCam;
public bool isRunning;
public bool isWalking;
// Start is called before the first frame update
void Start()
{
playerCamera = GameObject.Find("Camera").GetComponent<Transform>();
playerRb = GetComponent<Rigidbody>();
playerRb.freezeRotation = true;
Physics.gravity *= gravityModifier;
playerCam = GameObject.Find("Camera").GetComponent<Camera>();
}
// Update is called once per frame
void FixedUpdate()
{
playerMovement();
transform.rotation = Quaternion.Euler(0, playerCamera.transform.eulerAngles.y, 0);
}
void Update()
{
playerJump();
zoomIn();
}
void playerMovement()
{
isWalking = false;
isRunning = false;
if (moveDirection != Vector3.zero)
{
if (Input.GetKey(KeyCode.LeftShift))
{
speed = 10;
isRunning = true;
}
else
{
speed = 5;
isWalking = true;
}
}
float horizontalInput = Input.GetAxisRaw("Horizontal");
float verticalInput = Input.GetAxisRaw("Vertical");
// Get the forward and right vectors from the camera
Vector3 cameraForward = playerCamera.transform.forward;
Vector3 cameraRight = playerCamera.transform.right;
// Remove the y-component from forward and right, since we want 2D movement relative to the ground
cameraForward.y = 0;
cameraRight.y = 0;
// Normalize the vectors
cameraForward.Normalize();
cameraRight.Normalize();
// Calculate movement direction based on camera's forward and right
moveDirection = (cameraForward * verticalInput) + (cameraRight * horizontalInput);
moveDirection.Normalize(); // Normalize to ensure consistent movement speed
RaycastHit wallDetect;
if (Physics.Raycast(transform.position, transform.right, out wallDetect, 1.1f))
{
if (wallDetect.collider)
{
playerRb.velocity = new Vector3(0, playerRb.velocity.y, 0);
return;
}
}
playerRb.velocity = new Vector3(moveDirection.x * speed, playerRb.velocity.y, moveDirection.z * speed);
}
void zoomIn()
{
float targetFOV = 30;
float velocity = 0;
float currentFOV = playerCam.fieldOfView;
if (Input.GetKey(KeyCode.Mouse1))
{
playerCam.fieldOfView = Mathf.SmoothDamp(currentFOV, targetFOV, ref velocity, 0.06f);
}
else
{
playerCam.fieldOfView = Mathf.SmoothDamp(currentFOV, 60, ref velocity, 0.06f);
}
}
void playerJump()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded())
{
playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
public bool isGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, 1.1f);
}
}