Wallrun making me fly

I was just finishing up a wall run script from a guide when all of a sudden, the wall run is making me run on air? It is effectively just making me float around, any ways I can make it not make me fly?

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

public class Wallrunning : MonoBehaviour
{
    //Stating all of the things.
    [Header("Wallrunning")]
    public LayerMask whatIsWall;
    public LayerMask whatIsGround;
    public float wallRunForce;
    public float wallJumpUpForce;
    public float wallJumpSideForce;
    public float maxWallRunTime;
    private float wallRunTimer;



    [Header("Input")]
    private float horizontalInput;
    private float verticalInput;



    [Header("Detection")]
    public KeyCode jumpkey = KeyCode.Space;
    public float wallCheckDistance;
    public float minJumpHeight;
    private RaycastHit leftWallHit;
    private RaycastHit rightWallHit;
    private bool wallLeft;
    private bool wallRight;

    [Header("Exiting")]
    private bool exitingWall;
    public float exitWallTime;
    private float exitWallTimer;


    [Header("References")]
    public Transform orientation;
    private PlayerController pm;
    private Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        pm = GetComponent<PlayerController>();

    }
    private void Update()
    {
        CheckForWall();
        StateMachine();
    }

    private void FixedUpdate()
    {
        if(pm.wallrunning)
            WallRunningMovement();
    }

    private void CheckForWall()
    {
        wallRight = Physics.Raycast(transform.position, orientation.right, out rightWallHit, wallCheckDistance, whatIsWall);
        wallLeft = Physics.Raycast(transform.position, -orientation.right, out leftWallHit, wallCheckDistance, whatIsWall);
    }
    //Raycast to check for wall is above and below this message.
    private bool AboveGround()
    {
        return !Physics.Raycast(transform.position, Vector3.down, minJumpHeight, whatIsGround);
    }

    private void StateMachine()
    {
        //Inputs.
        horizontalInput = Input.GetAxisRaw("Horizontal");
        verticalInput = Input.GetAxisRaw("Vertical");

        if ((wallLeft || wallRight) && verticalInput > 0 && AboveGround() && !exitingWall)
        {

            if (!pm.wallrunning)
                StartWallRun();

            if (Input.GetKeyDown(jumpkey)) WallJump();

        }

        else if (exitingWall)
        {
            if (pm.wallrunning)
                StopWallRun();
            
            if (exitWallTimer > 0)
                exitWallTimer -= Time.deltaTime;

            if (exitWallTimer < 0)
                exitingWall = false;
        }
        
        // New state????
        else
        {
            if (pm.wallrunning) 
                StopWallRun();
        }

    }

    private void StartWallRun()
    {
        pm.wallrunning = true;

    }

    private void WallRunningMovement()
    {
        rb.useGravity = false;
        rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.y);

        Vector3 wallNormal = wallRight ? rightWallHit.normal : leftWallHit.normal;

        Vector3 wallForward = Vector3.Cross(wallNormal, transform.up);
        //Adding force now.
        rb.AddForce(wallForward * wallRunForce, ForceMode.Force);

        if (!(wallLeft && horizontalInput > 0) && !(wallRight && horizontalInput < 0)) 
            rb.AddForce(-wallNormal * 100, ForceMode.Force);
    }

    private void StopWallRun()
    {
        pm.wallrunning = false;
    }

    private void WallJump()
    {
        exitingWall = true;
        exitWallTimer = exitWallTime;
        Vector3 wallNormal = wallRight ? rightWallHit.normal : leftWallHit.normal;

        Vector3 forceToApply = transform.up * wallJumpUpForce + wallNormal * wallJumpSideForce;

        rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.y);
        rb.AddForce(forceToApply, ForceMode.Impulse);
    }
    

}

Debug the cause of this, then fix the bug. These forums are not your debugging service.

I am here because I cannot find what is causing it, the forums are to help me if I have any issues. So if you cannot be helpful, just don’t respond.

What have you tried so far?

We won’t be able to tell by simply looking at a pasted wall of code either. The only way any of us would be able to debug this is having the project there in front of us. And the person who has the project in front of them is you.

If your character is always wall running, then check the parts of your code that engages the wall run and see if that is behaving correctly.

Well, I have checked, so there is something doing it.
I just don’t know what.

What debugging have you done so far?

Then you haven’t finished debugging. The process of debugging is to find out what is wrong. Use Debug.Logs or attach the debugger to step through your execution. Like I said, if your player is always wall-running, then the relevant code to that is where you should start debugging.

And I am telling you, I cannot find the issue and so I have no idea what to fix.

It’s not about knowing what to fix, it’s about first finding what is wrong. Until you find what is wrong, you can’t take the steps to fix it.

What debugging steps have you taken so far? What testing have you done?

I have been disabling stuff and checking in both unity and visual, I cannot find what is making me do it without making the wallrun not work at all.

That’s not really how you debug code. You actually need to step through the execution, understand what the code and related values are doing and how that leads to an undesired result. Once you understand what is going on, then you can start looking at how to fix it.

Debugging is either done by using Debug.Log calls to print out meaningful information and values about relevant parts of the code. Or you can attach the Debugger and actually step through the code, line-by-line, and see how it executes: Unity - Manual: Debug C# code in Unity

With scripts like this that are part of a set of scripts and have lots of parameters it’s very difficult for us to help you debug them. But glancing over your script I do see that on line 120 you disable gravity and yet I don’t see anywhere that re-enables gravity after your player has left the wall. Try re-enabling gravity in the StopWallRun() method.

1 Like

I have added it to be re-enabled, I stop floating now but now I cannot wall run at all. Same code but now I cannot wall run.