Wall detection using Rays

I am making a 2.5D platformer controller and I am manipulation the player using transform.translate. I’ve been taking the script from this tutorial almost exactly, minus the animation portion and sliding: Unity Platform Game Tutorials - YouTube

  • I use a function that takes in the current speed, target speed and an acceleration to calculate how much to move the character in order to achieve acceleration and a bit of a sliding motion when changing directions.

  • I pass the amount to move the character into a variable

  • Then finally pass that variable into another function that detects floors and ceilings using rays to prevent the player from clipping through walls

(most of this is explained in the first two videos)

The plan was to modify the wall jump script he used in video # 6, but there was a problem when sliding down walls that did not have a ledge below it. The player would continue a slow decent despite there not being a wall anymore.

The first thing I change was to make the physics function make horizontal detections even if there is no horizontal inputs; but that lead to another issue that I can’t pin down.

  • the player will now only wall slide if you hold the directional key toward the wall!!!

I have checked the x transform to ensure that the player isn’t moving unintentionally, but for some reason the wall detection just stops unless you hold the directional key toward the wall. The absolute worse part is that it only stops some of the time.

Is this a limitation of using Rays? Or is something else going on? Any help would be greatly appreciated

I have found a fix! First I made horizontal detection possible even if there is not input. Then in the PlayerPhysics script where it checks for wall collisions, I created another if() statement.

if(Physics.Raycast(ray, out hit, 0.006f, collisionMask))
{
        if(hit.collider.tag == "Wall Jump" && !grounded)
        {
                canWallHold = true;
        }
}

This was necessary because if there was no horizontal input, the original ray distance check would not reach far enough to detect the wall which is why the distance I checked was just a little bit longer than the skin’s distance.

Then in the PlayerController script I also changed all the checks for wall holding to one if() statement

if(playerPhysics.canWallHold == true)
{
        wallHolding  = true;
}
else
{
        wallHolding = false;
}