# Creating an autostep / Looking for an empty space (3D)

4.2

### Question

I am making a 3D game project, in which the world is made of cubes.

So i want to have an autostep :

if the player walks toward a cube that’s 1 Y above the base of the player, the player should continue its course up to the cube. If the cube was 2 Yabove the player, it shouldn’t work. If the cube is 1 Y above the player but the space above the cube is occupied by another cube, it shouldn’t work.

Its a classic autostep as viewed in many videogames.

However Im failing at those 2 last points, here’s my script :

``````func _on_autostep_body_entered(body):
var heightDifference = body.global_transform.origin.y - (global_transform.origin.y-0.4)
if heightDifference >= 0 && heightDifference <= autostep_height:
global_transform.origin.y += 1
``````

My autosteps works with 1 cube, but it also does with more cubes because the code is just executed multiple time and the player could end up teleported 20 cubes above. Also, I didn’t find the answer to check if the space above the cube was free or not.

I hope this was clear enough, I’m up to give as much informations as possible.
Thank you kindly !

What I see here is if the height check returns false nothing happens. Is the expectation that the player object should remain in the lower cube space? Are there collision boundaries preventing the move? What are the collision shapes? What are the scales? Are these physics bodies character nodes? Or areas?

If the check is successful instead of `+= 1` you should just use `= body.global_transform.origin.y` so the multiple collisions don’t just keep adding to the height.

To solve your last problem where the space is occupied you may need to have a grid terrain class that knows if something is occupied. So you can extend the check height and if space is occupied.

Is the player’s collision shape taller than the cube? If so, what might be happening is that you move the player up one cube, but this means that the top of the player enters the next cube, so the player gets lifted up again, and so on and on and on.

If this is indeed the problem, then you can add another area/body with a smaller collision shape to the player, and use collision layers/masks to have your cubes only interact with that layer.

As for checking if the space within a cube is free: If your cube has an Area3D, then you can use its `has_overlapping_areas` and/or `has_overlapping_bodies` methods to see if anything is in that area. There is also `get_overlapping_areas` and `get_overlapping_bodies`, if you need information about what exactly overlaps with it.

I want to do the exact same thing as in cubeworld.

The player object should go above a cube.
There are collisions that blocks the player to go through cubes.
The collision shapes are cubes.
The scales are 1x1x1m for the cube, the player is a default capsule shape of 0.5m radius and 2m height.
The player is a CharacterBody3D, the cubes are StaticBody3D.
The player have area3D for the collisions.

body.global_transform.origin.y is not working since the block is at the same height than the player (the role of the autostep) but += 1 is fine

I don’t know how to manage a grid terrain since it could be very costy, how does Minecraft or Cubeworld handle it ?

Yes the player’s collision shape is taller than te cube, and I believe that’s what happening.

I’m not sure how I could make a smaller area3D work for the autostep, wouldn’t the problem be the same ?

My cubes don’t have area3d since it whats the map should me made of, and I believe that would be very costy to make every cube with area3D

If I think about Minecraft it doesn’t autostep. You need specific blacks that form a ramp or half blocks.

If you height code works then you just need a test to make sure it makes sense to autostep. (but I thought you said your character goes high into the air, I think this is the fault of +=1 math and multiple calls on the body collision callback. It would be easy to calculate a block global height and just set your y axis to that number. Then if multiple collisions happen you don’t go 20 m into the air, it just sets the height to the same value until you stop colliding)

If I were doing it… For the extra test, I would add a collision area3d (knee collision system) at a height you would allow a step to happen. 1 m + magic number that would pass over a block 1m high. If the block has another block or is occupied, the knee collider will hit the object and fail the test to autostep. Then your collision step code will do it’s thing if the knee didn’t collide with anything.

You probably need two or more “knees” depending on how complicated the geometry can get and if you want to autostep going backwards or sideways. Or whichever direction… Maybe just one knee that points in the direction of the players input.

As I think about the autostep more it might be smoother if you added a force to lift the player up. So it’s not a sudden position change.

Yes the autostep is really more accurate in Cubeworld, as it whats im aiming for. In minecraft there is more of an “auto jump” in vanilla, but there is an “auto step” in modded Minecraft, that works the same

Im not going that heigh into the air when Im only autostepping with one block, I only go above it as its intended, my problem is that if im facing a 2height block my character will still have the autostep since its area collide with a block, but it shouldn’t since the block above is not an empty space

But thank you i’ll try to add another area to test if other blocks are in it. Same for ce force to lift the player up it should render better !

1 Like

I went and checked out cube world. It almost seems like the character is clipping through the block a little, but is otherwise unimpeded by any collision.

And going down a blocks will be pulled down. Which makes me think of another system that someone else here was having issues with some physics, standing boxes with low mass would wiggle away. (Not really an issue with static bodies)

I think this could be another option for you.

The collision shape of the character would be above the block size. And when the character “steps” it is causing by sensing changes of a cast ray length. Which will apply some spring like forces to keep the player at a specific height. This would also help going down hill as well and prevent falling with the hope it looks more natural.