(2D) Check if player is near a corner

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Arkanis

How would you check if a player is very close to a corner?
In a platformer, usually a wall jump is done by raycasting from the player, check if he’s in the air, etc.

Imagine I want to have a “corner-jump”. The jump is much powerful than a normal wall-jump. How would I detect that the player is very close to a corner?

Of course, I would need to check if the player is on air first (not descending, but ascending). Then I could have another raycast, but at a 45ª degrees angle. Then have two checks: the horizontal raycast is not colliding + the angled raycast is colliding. This is not a very elegant solution, and I was hoping someone with more smarts than me could give me a hand.

Cheers

Mock up

:bust_in_silhouette: Reply From: Gluon

I would suggest that on the platform node you add an area2d on the corners which sends out a signal when the player enters it. Then in the player script you can listen out for that signal and if your player is in the area and the signal is received you can check if the player is going up and then do your corner jump code if it is.

Hi Gluon, thanks for the answer.
I thought about it, but it’s even less elegant than the one with the raycasts. I’m “hardcoding” the areas, and I don’t want to do that for a variety of reasons.
The only way I could do it, right now, is using raycasts. Then, when the check becomes true, if you jump in those few frames you get a corner jump, otherwise nothing happens.
But I’m sure there must be a better solution…

Arkanis | 2022-12-11 00:06

Okay not sure why you wouldnt want to do it with area2d’s but its your game. Not sure how you would do it with a raycast though, you would need to figure out how to add a timer to the raycast but how you would then make sure the double jump could only happen during that time is much more complex. Seems to be needlessly complicating the problem in my mind but I guess you would need to work something out with a series of boolean guards which are changed from false to true based on timers and the interaction of raycasts with the platform. Sounds like a messy way to achieve the same solution though.

Gluon | 2022-12-11 00:13

It’s quite easy actually.

  1. I check if the player is in the air, and is ascending. True? Go to 2.
  2. I check if the horizontal raycast is touching a wall. False? Go to 3.
  3. I check if the angled raycast is colliding with something. True? Go to 4.
  4. Start a timer of few frames, where you can press jump again.

I was doing it today at the office and it kind of worked, but I don’t like it, need to test it more. Maybe with some adjustments it could work, but I was wondering if there was an easy way.

I don’t want to do it with the Area2Ds because I plan on making loads of levels and eventually a level editor. The jumps shouldn’t be dependent from how the level is built, just by the corners themselves.

Arkanis | 2022-12-11 00:18