Home > other >  Unity - Blocking character from walking off ledges
Unity - Blocking character from walking off ledges

Time:09-04

I don't want my player to be able to walk off ledges. I did this by shooting a single raycast downwards in front of the player, and if ground is NOT hit, then ignore input.

However this is jarring, especially if you diagonally walk along an edge you just completely stop, rather than 'slide' along it.

So I thought I could add two raycasts, one per side to detect which side the ledge is, then allow movement (or steer) the player as applicable.

enter image description here

The problem is I'm not sure how to proceed from here. I'm using a character controller for movement, my current code is like:

velocityXZ = velocity;
velocityXZ.y = 0; // we deal with gravity elsewhere
velocityXZ = inputDir * playerSpeed;

if (facingDropLeft || facingDropRight) {
    velocityXZ.x = 0;
    velocityXZ.z = 0;
    }
velocity = new Vector3(velocityXZ.x, velocity.y, velocityXZ.z);

// handle gravity

charController.Move(velocity * Time.deltaTime);

Could anyone offer some insights into what direction to look into, or methods I will need?

CodePudding user response:

I think that if you want to accomplish an enjoyable result you should use invisible walls. I think that probuilder can help you. This is the approach I would have with this type of problem.

CodePudding user response:

Use boxes to make wall than turn off the mesh renderers this will make invisible walls

  • Related