r/howdidtheycodeit • u/Hungry-Particular-97 • Jan 08 '23
Question How does one code a defense mechanic like Paper Mario?
In Paper Mario, when an enemy attacks you, you have a window of time to press the block button to successfully block the attack. If you get it spot on, you block the max amount. If you get close to the proffered time, you still block, but a little less. Your attack is a similar situation. During your attack animation, there is a few seconds before the impact where you can press the action button to deal more damage.
Is this driven via the animation events per animation? Paper Mario Combat Tutorial - RIP Professor Frankly
22
u/BisonST Jan 08 '23
Not a programmer, but I imagine there are a set number of frames to hit the button. Too late and the game ignores the input and the normal damage was already applied.
11
u/LazyIce487 Jan 08 '23
Haven’t seen it in action, but if you initiate the animation and you measure it in frames or time, you just measure the distance from “perfect” timing and the users input. And divide based on that, i.e., if the timing was within 100ms or X frames of “perfect” you award max, and you pick the scale/window from there outward
4
u/Asnyd421 Jan 08 '23
Yeah I'd start a timer when that bar appears, when the timer ends the attack proceeds with no bonus. Then make an event that receives user input, it'll want to grab the current remaining time and stop the timer. Then use that value to LERP between whatever the min damage modifier and the highest damage mod (maybe .5-1.5). Multiply damage dealt by that.
It'll be a little tricky mathematically because after that peak point the LERP will have to invert so you'll end up with an if( alpha > peakDamageSpot) { LERP from 1.5 to. 5} else { LERP from. 5 to 1.5}
Sorry for the formatting on mobile
3
u/[deleted] Jan 08 '23
During the attack animation you set a boolean that is being checked on button press, allowing the blocking logic to run.
1
u/snipercar123 Jan 11 '23 edited Jan 11 '23
This looks like something you can set up to work in a generic way using events and a timer.
I wrote some code from my phone, C# Unity.
1
u/InfComplex Jan 16 '23
Right now I’m working in Unreal Engine, so my solution makes use of timelines.
When the enemy’s attack begins, a 0.0-1.0 timeline begins from the start. When the player is intended to block, a bool is set to true. That bool blocks an event that stops the timeline. Read value, apply to damage scale.