#Prevent Wallrun

1 messages · Page 1 of 1 (latest)

fiery bolt
#

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:

#
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

        if (!isGrounded())
        {
            // Activate raycast if the player is mid-air
            RaycastHit wallHit;

            if (Physics.Raycast(transform.position, moveDirection, out wallHit, 1.1f))
            {
                // If a wall is detected, stop horizontal movement but allow falling
                float wallAngle = Vector3.Angle(wallHit.normal, Vector3.up);

                if (wallAngle > 75f && wallAngle < 105f) // Check for steep walls
                {
                    playerRb.velocity = new Vector3(0, playerRb.velocity.y, 0); // Stop horizontal movement
                    return;
                }
            }
        }

        playerRb.velocity = new Vector3(moveDirection.x * speed, playerRb.velocity.y, moveDirection.z * speed);
    }```
timid fable
#

Showing a video of the issue would be good

fiery bolt
#

ok wait

#

imma record a one rn

timid fable
#

Also, is that ChatGPT code? Do you understand the code?

fiery bolt
#

thats my code yea but i am not like advanced programmer xd

#

i kept asking chatgpt for help about the wall run issue so it kept changing my code

#

originally this was my code:

if (!isGrounded())
        {

            RaycastHit wallHit;

            if (Physics.Raycast(transform.position, moveDirection, out wallHit, 1.1f))
            {
 
                    playerRb.velocity = new Vector3(0, playerRb.velocity.y, 0); 
                    return;
                }
            }
        }
fiery bolt
#

so @timid fable sorry for pinging very sorry

timid fable
fiery bolt
#

lemme try this

#

this didnt even come to my mind when i was thinking

#

omg

#

it worked

#

holy

#

@timid fable

#

this worked

timid fable
#

Nice

fiery bolt
#

thanks so much

timid fable
#

Np

fiery bolt
#

appreciate ur help