Raycast not detecting object midair

Godot Version



I am creating a 2d platformer in which you are able to pickup, throw, and jump on boxes. In this game you can jump on a box midair if it is within a “slowing field.” The player is able to pull the box closer to them via a button. The intention is to not let the player jump, pull the box closer to them mid air, jump again, pull the box closer to them, and repeat this process essentially making the player fly. The jumping code works perfectly aside from one issue. I am using a Raycast2D that’s position is the bottom left of the player and target is the bottom right of the character to check to see if the player is standing on the box when the player jumps. If the raycast is touching the box and the box is NOT able to be jumped on (only time the box can not be jumped on is when it is midair and not within a “slow field”) then the player will not jump. This works well except if I am able to jump on the edges of the box while it is not able to be jumped on. I have done lots of different debugging with it and believe the issue has to due with the code that updates the raycast’s position is not being executed fast enough. The only problem is I don’t know why it is happening! Attached is the code and I do not understand why the player is able to jump when the position of the raycast is updating before the actual act of jumping occurs. What I REALLY do not understand is why the player is only able jump on the edges of the box midair. I have tried changing the size of the Raycast and have ruled that out as an issue, the raycast has hit_from_inside set to true as well so it has no issues with detecting it. I have found a fix by putting the position update for the raycast outised of the if statement and simply within the process function, but I do not want to keep updating the position every frame for performance issues. The problem is not with the box’s jumpable variable as I have checked it. I am looking for an explination as to why this code does not work as intended and if there is a fix to keep the lines where the raycasts position is updated within the if statement. Thank you!

The $RayCast2D’s target_position is being set horizontally, it won’t detect boxes under your player only to the right of them, is that intentional? Maybe the boxes just count as is_on_floor? Since your raycast’s position is a vector2 literal can you just set it’s position and target_position in the editor, if you need to move it around you should make another raycast instead of moving this one.

The way the ray cast is set up allows it to check what is directly below the player when checked for what it collides with. I have hit_from_inside set to true and when I print what it collides with it returns with the box whenever the player jumps on it anytime other than on the edges of the box while it is midair.

I have tested by making the raycast longer or farther down however it does not fix the issue.

In regards to not moving it within the code I can not figure out a way for it to move with the player otherwise. I have not looked into creating a new raycast each time, but I will look into that now!

Since it’s a child of the player it should move with them. Your code actually doesn’t move the raycast since it’s position is being set to the same vector2 literal anyways.

Do not make a new raycast for each jump, I only recommend a new raycast if you are using this one for more than jumping and swapping it around with another $Raycast2D.position = etc in your code.