C
C#3d ago
Theos

Calculate if jump is possible

Hey, I'm working on a 2D grid game (think Terraria), I want to check if a monster can make jump from point A (start) to B (target). It can adjust its horizontal/vertical speed to hit smaller/bigger jumps, same as the player can (for example by holding space for longer to jump higher). So I'm calling this function CanMakeJump(monster.gridPos, target.gridPos, maxJumpPower, someGravityValue) I have no clue if my formula is even right, I'm getting weird results partially because I don't know how to incorporate horizontal velocity which should be based on monster's movementSpeed statistic. Code: https://pastebin.com/7WT8ULBS
Pastebin
```csprivate float CanMakeJump(Vector2Int start, Vector2Int target,...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
15 Replies
ACiDCA7
ACiDCA73d ago
you might have more success finding help in unity server $unity
Theos
TheosOP3d ago
yup, already asked there but didn't receive any answers
ACiDCA7
ACiDCA73d ago
well to me it looks like you are bruteforcing the solution.. but to my understanding if you want to jump from a to b you can just follow basic math formula and then check if the calculation is inside possible jumppower would be couple of lines without loops instead of your solution the only thing you might want to check aswell then would be if you bump your head on the jump trajectory
Theos
TheosOP3d ago
but I still need to bruteforce some solutions check this case:
Theos
TheosOP3d ago
No description
Theos
TheosOP3d ago
there is a lot of possibilites on how this jump can be done some are valid and some not
ACiDCA7
ACiDCA73d ago
since i dont see any jumpforwardvelocity that affects how far forward the jump is i asumed the distance jumped is proportional to jump height so either the lower one would never reach the target or the higher one overshoots
Theos
TheosOP3d ago
since i dont see any jumpforwardvelocity
What do you mean? I don't have that in code, if thats what you are saying. I wasn't sure how to include it in the calculations
ACiDCA7
ACiDCA73d ago
lets say from standstill jump 1 meter high, you get 1 meter far, when you jump 2 meter high you get 2 meter far.. so now you do the jumping while running your 1 meter high jump now gets you 2 meters and you 2 meter jump 4 meters... <- this is what i mean by jumpforward velo and like i said above i dont see that anywhere in your code so only option is a "standstill" jump which is calculable.. eg if i take the values again.. if i want to jump 1.5m far i have to jump 1.5m high this is obviously super simplified, but the thought should hold
Theos
TheosOP3d ago
I guess this could work for now, but I want to push it further to make it work in a more sophisticated way I’m trying to make AI slightly more engaging to fight I have pretty nice behavioral system in place, but this math kills me
ACiDCA7
ACiDCA73d ago
doesnt unity have a navmesh equivalent for 2d? im not a gamedev therefore legitimate question if it has doesnt it provide the necessary funtionality?
Theos
TheosOP3d ago
It has navmesh, but its useless in this case
ACiDCA7
ACiDCA73d ago
maybe a different plan instead of doing arbitrary calculations, you do baked ones.. but that would only allow jumping over predetermined obstacles dunno what your other requirements look like
Theos
TheosOP3d ago
player can break/place tiles so that won't work here

Did you find this page helpful?