CharacterBody3D vs stairs
Asked Answered
E

15

0

I'm wondering how you guys implement climbing stairs with a characterbody3d.
I guess there is no build in solution for this?

I thought of putting a raycast going downwards in front of the players movement direction (so he can climb stairs in any direction) and then moving him up. I haven't tested it yet, but I think a single raycast doesn't work in 3d as I need to know if there is enough room for the player to fit in smaller areas.

Some hints how to solve this would be nice.
I don't want to make ramp colliders on stairs btw. If I want rigidbody things to correctly fall down or apply decals I would have to add 2 collision shapes to it I guess.

When I noticed that my character doesn't climb stairs I thought of this:

Ephah answered 18/11, 2023 at 18:10 Comment(0)
T
0

Ephah If I want rigidbody things to correctly fall down or apply decals I would have to add 2 collision shapes to it I guess

I think that is usually how it's done. A simple plane ("ramp") for the nav mesh that the characters use for walking around and then a more detailed collision mesh for physics related stuff like ragdolls or bullet holes.

Thoughtful answered 18/11, 2023 at 18:21 Comment(0)
F
0

Ephah I don't want to make ramp colliders on stairs

Why? Doing it any other way doesn't make much sense.

Forfar answered 18/11, 2023 at 18:47 Comment(0)
T
0

You can use a ramp, that's the simplest solution.
In my case I made a stair collision layer and have a ray at player feet level, the ray is aimed forward (multiplied by input direction and player facing), and when it detects a slope, and there's input, it pushes the player up.
It works, you can't tell the difference. But you need to be careful about collision layers and static physics nodes in your scenes.

Tauto answered 18/11, 2023 at 19:44 Comment(0)
S
0

Even AAA companies often use ramps for stairs. There will be collision boxes for the steps as well, but on a different layer for use in IK animation. Not only is a ramp an easier solution, but it also simplifies the physics situation which is good for performance

Shaunna answered 19/11, 2023 at 10:7 Comment(0)
E
0

Forfar I see that now. But like Stylo wrote you need a second collision shape and layer for handling other physics and foot ik. So I wasn't entirely sure whether or not to use one complex collision shape or two, one being a ramp for character movement.
But I guess you need to implement some stair handling as well. You can't force the player to jump on sidewalks etc.

Ephah answered 19/11, 2023 at 15:36 Comment(0)
F
0

Ephah Well it depends on the type of the movement/game. Ramp is the best option for main movement collisions. Eventual individual stair colliders are basically for looks/effects.

Forfar answered 19/11, 2023 at 16:5 Comment(0)
T
1

Shaunna that is not true. Character controllers tend to have support for stairs.
Bge had stair support, unity had it, source had it, unreal probably has it.
Ramps are mostly used with games that use a physics body for the player, as oposed to a character controller.
It depends on the game, the level design. Some parts can't be faked with ramps.
I gave a solution, it's very easy if you know vectors and keep your level well organized. Maybe I should post some code.

Tauto answered 19/11, 2023 at 16:28 Comment(0)
T
0

Tauto unreal probably has it

It doesn't. Although I am not exactly sure what you mean by "have support for stairs".

You are basically just pushing a collision capsule around and the 3D model and all the animations are kind of like a ghost pantomime sitting in a bird cage. By default they don't react to the world at all. Only the capsule does. I made a test a while ago and all the foot IK stuff you see in that video I had to do myself. (Or at least figure out placement (height) and rotation of the foot. Unreal handles the bone IK.)

You can set parameters for the capsule like the maximum angle it can climb or the maximum step height but that's it. There is no special build-in support for steps.

Thoughtful answered 19/11, 2023 at 17:16 Comment(0)
T
0

Thoughtful maximum step height but that's it. There is no special build-in support for steps

That's exactly it. Godot doesn't have step height on character controller, it has good support for ramps through slopes, which is really good for 2D, but not enough for 3D. It does have some better thing than unity like is_on_ceiling and is_on_wall.

Tauto answered 19/11, 2023 at 17:28 Comment(0)
F
0

Stair handling is trivial to script for kinematic colliders.

Forfar answered 19/11, 2023 at 17:33 Comment(0)
P
0

So after searching around today trying to implement this in my game and not finding any really great examples, I worked out somewhat of a solution. So far it seems to work well and allows for a threshold to kind of mimic the way it works in Unity. It's written considering you're using a RayCast3D node as a child of the CharacterBody3D(in this case, Pivot is child of CharacterBody3D to handle camera rotation, StairRaycasts is a container Node3D child of Pivot to add additional raycast checks if needed after more testing, and then HeightCheck is the RayCast3D). The parent StairRaycasts is positioned a hair forward from the CharacterBody3D's CollisionShape3D and it's Y Position is set to 1, only reason I'm noting all of this is due to the way the code works basing it off of being at that height. My character controller script uses playerMovementVelocity that is just a variable that gets passed into Velocity for MoveAndSlide() so this is simply increasing it's Y value for the distanceTo between the RayCast3D's position and whatever "floor" it's hitting, if anyone thinks it can help them out but need the full script to make sense of it I can post that but this should be pretty easy to make sense of just looking at the code. Keep in mind this is just something I'm playing with but so far it's handling little differences in terrain quite well and also worked great on a little set of test steps I made, also doesn't seem to cause any negative effect to how the CharacterBody3D handles slopes by default.

	// **** TEST CODE FOR STAIR RAYCAST
	RayCast3D testray = GetNode<Node3D>("Pivot").GetNode<Node3D>("StairRaycasts").GetNode<RayCast3D>("HeightCheck");
	Vector3 testraycollisionpoint = testray.GetCollisionPoint();
	float distanceto = testray.GlobalPosition.DistanceTo(testraycollisionpoint);
	var collidedwith = testray.GetCollider();
	GD.Print("distance to ray collision " + distanceto);
	GD.Print("collided with " + collidedwith);
	// 1 seems to be a solid value when walking on moveable ground, goes to 0.78 when running into step that can't be climbed
	// will need to add threshold to compare 1(normal value) to (how far below 1 to allow step up)
	// USING 1 FOR THE Y POSITION OF RAYCAST PARENT HITS GROUND AT 1.00XX, THRESHOLD SHOULD BE IN 0.XX VALUE
	float threshold = 0.5f;
	//if (distanceto < threshold)
	if (distanceto < 1f && distanceto > threshold)
	{
		GD.Print("should climb step here");
		// SHOWS THAT STEP SHOULD BE CLIMBED
		// ***** WILL PROBABLY NEED TO ADD CODE TO PREVENT THIS FROM RUNNING WHEN MOVING SMOOTHLY UP A SLOPE ****
		playerMovementVelocity.Y = distanceto;
	}
Parve answered 25/11, 2023 at 6:54 Comment(0)
E
0

Parve I did something similiar.
It probably needs some fine tuning (and smoothing) but so far it works as expected.
Is it working well with changing the Y velocity? I didn't even think of that. I just changed the players Y position (that's the part that probably should be smoothed a bit).
Now that I see your comment about slopes: That's something I have to test. I totally forgot about slopes!😳

@export var step_height := 0.35
@export var stair_ray_offset := 1.1

func handle_stairs(direction: Vector2):
	stair_ray.position.x = direction.x * stair_ray_offset
	stair_ray.position.z = direction.y * stair_ray_offset

	if player.stair_ray.is_colliding():
		var collision_point = stair_ray.get_collision_point()
		var height = collision_point.y - player.global_position.y
		if height <= player.step_height:
			player.global_position.y = collision_point.y

EDIT: Okay, for the slopes I will check the normal direction of the ray hit point to ensure that the character only steps on actual stairs.

Ephah answered 25/11, 2023 at 19:31 Comment(0)
A
0

even in games as advanced as this thing that's been floating around, the stairs STILL use slopes as collision shapes.

there is no escaping the fact computers are too stupid to comprehend stairs.

i apologize for posting IGN on our sacred grounds. i'll never do it again. :(

Ailsun answered 25/11, 2023 at 19:54 Comment(0)
F
0

Ailsun STILL use slopes as collision shapes

In first person perspective, slopes feel much better and there's really no need to simulate stairs. For third person, it's debatable, depends on the style of movement, camera ranges and gameplay. But I think most games would be better off without it.

Forfar answered 25/11, 2023 at 20:3 Comment(0)
M
0

Forfar If you have something like a Voxel world, there are no predetermined collision shapes such as ramps, only cubes.

Melee answered 9/2 at 1:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.