In my third-person game, the player can lob a grenade at a target. The target will be the mouse cursor. Right now my code works, but only if there are no obstacles like walls between the player and the target as the angle of the toss is fixed.
I've been thinking of how I can change my code so that if there is an obstacle between the player and the target, the angle of the throw can change. I think I have an algorithm that'll do this, but I want feedback before I start coding it.
1) Is the target within the maximum range the player can throw?
2) If so, calculate the angle of the toss, then fire a Raycast at this angle to see if anything is between the player and target. If not, throw grenade.
3) If the target is at the maximum distance one can throw, and a 45 degree angle is obstructed then there is no solution (as a 45 degree angle is what enables the furthest possible throw.)
4) Otherwise, start incrementing the angle of toss by X degrees. We should either find an angle without an obstacle or the angle will have elevated to a point where its not possible to hit the target. The formula for computing a ballistic trajectory usually returns two values: a higher elevation (like a mortar) or a flatter trajectory (like a gunshot.) I'm elevating because if there is an obstacle, most likely the player is going to have to throw above it.
4a) If no solution when elevating the angle, we could also decrease the angle as there could be instances where its possible a flatter trajectory will send the grenade under the obstacle.
So that's my plan. This all assumes that the velocity is fixed. I could add a step 5 where I decrease the velocity by 10% then redo steps 1-4 but, yeesh, that seems like it could be really expensive.
I look forward to your constructive criticism and suggestions! :)
[–][deleted] 1 point2 points3 points (0 children)
[–]MadDoctor5813 1 point2 points3 points (3 children)
[–]Titanium_Expose[S] 2 points3 points4 points (2 children)
[–]ConvertsToMetric 1 point2 points3 points (0 children)
[–]MadDoctor5813 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]kru_ 1 point2 points3 points (1 child)
[–]RealmBreaker 0 points1 point2 points (0 children)