Wallrunning in C#, How to achieve it?

I’m making a simple FPS game to play with my friends, and i want to implement a wall running function into my script. Idk how to make it work properly, for now at least i have 2 colliders set as triggers on my main player and tagged as “WallRunLeft” and “WallRunRight”. both these objects use the same script, so i check if these colliders collide with a “RunnableWall” and if so, i compare the tags and make them work differently, for now, i have made the camera tilt towards the wall by using simple code, but i do not know how to achieve a proper wall running effect by smoothing the camera out, disabling gravity (yes i know i have done that in the script, and it says in the inspector that the Rigidbody is not using gravity but it still uses it anyway in the actual game) and applying forces to the player when he wall jumps off of the wall. I tried doing it but it didnt work.
This is my player movement script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(CharacterController))]

public class PlayerMovement : MonoBehaviour
{
    public float walkingSpeed = 7.5f;
    public float runningSpeed = 11.5f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public Camera playerCamera;
    public float lookSpeed = 2.0f;
    public float lookXLimit = 45.0f;

    CharacterController characterController;
    Vector3 moveDirection = Vector3.zero;
    float rotationX = 0;

    [HideInInspector]
    public bool canMove = true;

    void Start()
    {
        characterController = GetComponent<CharacterController>();

        // Lock cursor
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void Update()
    {
        // We are grounded, so recalculate move direction based on axes
        Vector3 forward = transform.TransformDirection(Vector3.forward);
        Vector3 right = transform.TransformDirection(Vector3.right);
        // Press Left Shift to run
        bool isRunning = Input.GetKey(KeyCode.LeftShift);
        float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
        float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
        float movementDirectionY = moveDirection.y;
        moveDirection = (forward * curSpeedX) + (right * curSpeedY);

        if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
        {
            moveDirection.y = jumpSpeed;
        }
        else
        {
            moveDirection.y = movementDirectionY;
        }

        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        if (!characterController.isGrounded)
        {
            moveDirection.y -= gravity * Time.deltaTime;
        }

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);

        // Player and Camera rotation
        if (canMove)
        {
            rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
            rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
            playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
            transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
        }
    }
}

and this is the wallrunning script i made:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Wallrunning : MonoBehaviour
{

    public Transform FPSCam;

    public float WallRunTurnAmount = 10f;

    public float JumpOffUpwardsPush = 100f;
    public float JumpOffUpwardsPushMultiplier = 1000f;
    public Rigidbody rb;

    void Start()
    {
      rb = GetComponentInParent<Rigidbody>();
    }

    void OnTriggerEnter(Collider other)
    {
      if(other.transform.CompareTag("RunnableWall"))
      {
        if(transform.CompareTag("WallRunLeft"))
        {
          FPSCam.Rotate(0, 0, -WallRunTurnAmount );
        }

        if(transform.CompareTag("WallRunRight"))
        {
          FPSCam.Rotate(0, 0, WallRunTurnAmount);
        }

      }

    }
    void OnTriggerStay(Collider other)
    {
      if(other.transform.CompareTag("RunnableWall"))
      {
        if(transform.CompareTag("WallRunLeft"))
        {
          rb.useGravity = false;
        }

        if(transform.CompareTag("WallRunRight"))
        {
          rb.useGravity = false;
        }
      }
    }
    void OnTriggerExit(Collider other)
    {
      if(other.transform.CompareTag("RunnableWall"))
      {
        if(transform.CompareTag("WallRunLeft"))
        {
          FPSCam.Rotate(0, 0, WallRunTurnAmount );
          rb.useGravity = true;
          rb.AddForce(0, JumpOffUpwardsPush , 0);
        }

        if(transform.CompareTag("WallRunRight"))
        {
          FPSCam.Rotate(0, 0, -WallRunTurnAmount );
          rb.useGravity = true;
          rb.AddForce(0, JumpOffUpwardsPush , 0 );
        }
      }
    }
}

need help pls

Hello @ruskoul3! Without seeing your entire project it’s hard to pinpoint where complex interactions are failing, but one thing that I can tell from your script is that gravity is being applied to your character manually in the PlayerMovement script on lines 59 and 63 instead of having the Rigidbody handle gravity forces. This is probably the reason why your character is still experiencing gravity after setting rb.useGravity = false;, because the Rigidbody wasn’t the one applying gravity to begin with.

The reason why your forces on the player may not be working is most likely because the Rigidbody is set to kinematic. The kinematic flag means that the rigidbody will be moved by some means other than physics, so if you want your object to respond to physics you would have to uncheck that flag. The fact that your PlayerMovement script is moving the player manually by coordinates and not by forces is an indicator that your Rigidbody is probably set to kinematic, in which case you would need to make it non-kinematic before apply forces and then make it kinematic again when landing on the ground to allow the player to control their movements again.

I hope that helps!

Bentley

@Bentley can you show the script