Crazy how the exact problem I have been banging my head against the wall trying to solve for the past couple days was in a Sebastian video the whole time. Incredible stuff!
This is probably one of the coolest ways to visualize and learn the use of the kinematic equations while studying math in school. Love this series and looking forward to more.
Ah, finally! I tried to use this to create a bomb-like weapon that fires in an arc last month, but I couldn't get it working, even though I had Physics in high school and wasn't too bad at it. Damn, I forgot a lot of the essential equations. It's all coming back to me now. I even managed to solve the question at the beginning of the video before continuing. Also, the debug path thing is bound to be useful. Thanks for the video, Sebastian!
@@mangomanlassi7779 I’m late, but I’d say physics is pretty useful in helping you understand a lot about things like kinematic equation as seen here but also to other things that will help you with realistic object collisions, etc
Holy shit, I've been searching for this kind of thing for so much time now and it turns Sebastian had such an awesome tutorial for this, this whole time!
I adjusted your code to calculate where the ball will land if the target were moving at a constant speed. Thanks to you, I actually completed my project!
Thanks for doing this tutorial. It would be cool to see how it works with a moving object. I think I can figure it out given this foundation, but you are the king and your tutorial would probably show me things I'm missing. Good job!
Just a heads up... if you set the Gravity to -60 ish, then the ball misses the target. Any idea why? rigidbody.velocity assign attempt for 'Ball' is not valid. Input velocity is { NaN, NaN, NaN }. UnityEngine.Rigidbody:set_velocity(Vector3)
omg i was going to suggest this as a video i thought would really suit your channel and I happened to be having some problems with it, just happened to glance through your videos and BOOM, thanks!
You are amazing! Your videos solve so many my problems during game developer work... i so love you! THANK YOU SO MUCH! I really try boost up my mathematics skills but some tasks is really difficult.... you are great...
Hi, You and Your solution are awesome ;) There is one small improvement that can be made, at least to Your code on github. I would call Mathf.Abs() on every math inside MAthf.Sqrt() and than it can also work if target is lower than ball :) Thanks again for this great video.
hey man , could you please do your change to code. obviously there is an issue with current one.whenever i move the target object and the curve is about to become a straight line the curve will be disappear and everything mess up ):
How would you go about using these equations to calculate the impact point of a thrown object? Every example I've seen predetermines the final position, but I want to know the landing point, given the initial velocity.
Vector3 DisplacementZ = new Vector3(0f, 0f, Target.transform.position.z - Ball.transform.position.z); Vector3 velocityZ = DisplacementZ/ (Mathf.Sqrt(-2 * height / Gravity) + Mathf.Sqrt(2 * (DisplacementY - height) / Gravity)); add these 2 lines of code and works in 3d, i mean when u move the object left and right
Thank you so much for this, the AI couldn't just help me with creating an equation that would work, but this not only teach me a lot of stuff but helped me solve my problem, thanks a lot!
Great video, I played around with it a bit. If I assume the ball is a soccer ball and add a reasonable drag coefficient(Cd) on the ball, say 0.25 - then the ball stops about half way. Is it possible to modify these equations to take into account the Cd ? Does anybody know how I might be able to do this ? (edit) The equations Sebastian provides is for 'high school' physics. When you add a drag coefficient onto your ball then the formulas don't work anymore. This is bc of air resistance (drag). There is an eq for air resistance but it is not a one time formula, you need to keep applying it per frame. Hence you cannot predict exactly where the ball will land. You can use integrals to get a fairly accurate path but in Unity it is not 100% accurate.
due thank you so much for making this. i was tryna implement somethig like this into my recroom game and i was having a brain aneurysm tring to put the equations in. Instant Like and sub
i think i saw one video where you show some of the games you made through the years, and there was a game i would like to know how to make it, its like a mouse cursor dodger, where you need to try click a moving target on the screen, the target will move around and away from your mouse cursor on a precise way, dodging your cursors velocity maybe...? it didnt look like randomly moving cause that would obviously make the dodger fail by walking into your cursor x y position... i didnt find it again, can you tell me about it?
Probably because he wants to put that value. As a programmer you can make your own Physics engine and that includes switching the real values for values you want. If you write the code in the video and change the gravity default value for -9.81 you will have a good result using real values.
For those want to change the velocity to get to the target, you can simply increase the gravity apply to the launch object. I am using this method in my 2D project and I don't want to change the whole Physics2D setting. So I make Rigidbody2D.gravityScale = gravity(in this video) / Physics2D.gravity.y;
Does anyone know how I would be able to implement a projectile-motion-like approach to this ? I have a very similar situation, but with a cannon, so the cannon's rotation needs to influence both the x and y velocities. I have already tried implementing the cos(angle) for the x speed and sin(angle) for the y speed, but it didn't work 100%...
Lovely tutorial. Except you didn't had to drive a formula for initial velocity for horizontal. You can directly use V = d/t and that's what your derivation ends up at.
I'm not sure whether you understood the purpose of this video. The ball's speed is actually being calculated in script so it always hits the target. Setting/adjusting the speed of the ball is easy.
Hi, I found a solution to some extent. You can decrease the gravity value to -40 for a quick ball and increase it to -5 for a really slow ball. This is not the best solution, but it works as long as you don't change the values too much.
Looking to create a script where a game object moves in a parabola from point A to point B , unlike this video where physics works, I just need it the game object to travel between those points, any help on that ?
so question. I wanted to get this equation to work in a unity project, but I am wanting to use it with a grenade, so I place an empty object in front of the player, and that is the endpoint and the player is the start point. I can not figure out how to convert it tho, any ideas?
Maybe someone solved this problem already but I cannot stick my head around it. I copied the code exactly as written in this tutorial but the center of my ball seems to miss the center of the target by about 0.5 on the x-axis. The calculated velocity is the same and I positioned all my gameobjects as in this scene... Anyone having an idea about this weird behaviour? I tried to watch this video in slow motion but I just cannot see wether the ball trajectory would hit the targets center given the colliders would be inactive.
I know this is an old video but how would you dynamically change the height of the arc based on the distance you are throwing it rather than have it as a set value?
This is the most thorough explanation of the subject I've seen so far. One question regarding the for loop when drawing the line: Is it a valid way for an AI to check if he can jump on a point (and not hitting the ceiling e.g.) or is there another way of doing it?
Hi Sebastian , I'm making a space exploration game in unity and the planet terrain has been stumping me for a while now and I was wondering if you could possibly help me at all or make a procedural planet series?
I think you'd need to pick a height, since an arrow can be shot in a curve between two points in a number of different angles, even in a straight line. If it were three points then you could use the middle one as the height
how to calculate the wind (x-only), for this formula: VelocityX = DisplacementX / (Mathf.Sqrt (-2 * h / gravity.y) + Mathf.Sqrt (2 * (DisplacementY-h) /gravity.y)); ? Who knows?
Hey Sebastian, awesome tutorial, I've been tinkering with this problem for a while now, you really helped me out, but I've one question, could you explain how I could apply this to a moving target?
Old but very nice tutorial, however, I can't figure out why you have taken the gravity as -18 m/s2 instead of -9,81 m/s2 as it is supposed to be. This deviated the simulation 1.4 times off of the reality.
The rate of fall is a constant irregardless of the forward velocity of the ball. So to get the ball to hit a fixed target simply make the arc shallower. e.g if target is at 9, make the highest point of the arc, H, 9.0001
If the ball is missing the center of the hoop by 0.5 to 1 unit, this *might* help: I haven't watched the full video, but I asked about this for the first video (Mario jumping) on the Unity forum, and while I didn't understand the full explanation, apparently it has to do with Unity using semi-Euler approach to physics and not calculating the velocity based on the first physics frame but the second instead (or something like that). Try adding "... + (0.5 * Time.fixedDeltaTime * (whatever your acceleration is, the gravity) * -1) * Vector(dot)one" to the initial Rigidbody2D velocity and see if that helps.
If you increase the gravity, the speed will be faster to compensate. If you decrease H (peak height) then the ball will be more direct and will have to go faster to counter gravity. But outside these constraints, going faster just means you miss the target in a purely physical simulation. One option is that instead of a Rigid body you could use this math to create an animation and you can play that animation at any speed you like.
Assuming you want to add also a curved trajectory horizontally instead of letting the ball fly horizontally a straight line. How would that work ? anyone ? oO
This is what I call a SIMPLE, CLEAR AND STRAIGHT TO THE POINT tutorial. Brilliant and crystal clear!
Crazy how the exact problem I have been banging my head against the wall trying to solve for the past couple days was in a Sebastian video the whole time. Incredible stuff!
You deserve to be in one of those "People are awesome" sort of videos
This is probably one of the coolest ways to visualize and learn the use of the kinematic equations while studying math in school. Love this series and looking forward to more.
You know when it comes to hard stuffs, we always end up on your channel! We really love what you do Sebastian! Thanks for having us indie's back!
May I ask what kind of game you're making?
@@CraftBasti I made some peojectiles using this
@@Zihan118 obviously, but what genre of game are we talking about? 3D RTS? 3D FPS? 2D Side Scroller?
CraftBasti bump.
Ah, finally! I tried to use this to create a bomb-like weapon that fires in an arc last month, but I couldn't get it working, even though I had Physics in high school and wasn't too bad at it. Damn, I forgot a lot of the essential equations. It's all coming back to me now. I even managed to solve the question at the beginning of the video before continuing.
Also, the debug path thing is bound to be useful.
Thanks for the video, Sebastian!
im planning to do physics next year, is it worth it?
@@mangomanlassi7779 I’m late, but I’d say physics is pretty useful in helping you understand a lot about things like kinematic equation as seen here but also to other things that will help you with realistic object collisions, etc
I'm addicted to game development videos now without people like you i'd be dead Thanks alot for doing these videos
Fantastic! I looked at several approaches that didn't work quite right before I found yours, and this calculation is PERFECT!
Holy shit, I've been searching for this kind of thing for so much time now and it turns Sebastian had such an awesome tutorial for this, this whole time!
I adjusted your code to calculate where the ball will land if the target were moving at a constant speed. Thanks to you, I actually completed my project!
How did you do that? I'm having trouble with that issue
Solved the problem I was struggling with for like 5 days.. Thank you so much.
You'r a genius, man! Fantastic, finally I found exactly what is needed!! Thank you!
Sebastian you saved someone's life today thanks man for the tutorial and alll the math you explained
Thanks for doing this tutorial. It would be cool to see how it works with a moving object. I think I can figure it out given this foundation, but you are the king and your tutorial would probably show me things I'm missing. Good job!
*Meant moving target, not object.
Just a heads up... if you set the Gravity to -60 ish, then the ball misses the target. Any idea why?
rigidbody.velocity assign attempt for 'Ball' is not valid. Input velocity is { NaN, NaN, NaN }.
UnityEngine.Rigidbody:set_velocity(Vector3)
This problem took me a couple years to solve for the Minecraft Mod: OpenBlocks's Canon. Thanks for a nice explaination - years later ;D
omg i was going to suggest this as a video i thought would really suit your channel and I happened to be having some problems with it, just happened to glance through your videos and BOOM, thanks!
Will you continue this series? I would like to see more and learn more in your way of teaching. It's really helpful
it is 1am and I finally understood, thank you for the video you make it easy to understand
You are amazing! Your videos solve so many my problems during game developer work... i so love you! THANK YOU SO MUCH! I really try boost up my mathematics skills but some tasks is really difficult.... you are great...
Amazing explanation sir :)
Thank you for making whole process simple.
Thanks, I feel like Isaac Newton now. Super satisfying to see the ball always hit the hoop perfectly :D
Thanks very much! Changed the code and using it for a 2D game. WIll use for a 3D game in the future. Many merits.
But not work for negative gravity :s
Hi, You and Your solution are awesome ;) There is one small improvement that can be made, at least to Your code on github. I would call Mathf.Abs() on every math inside MAthf.Sqrt() and than it can also work if target is lower than ball :) Thanks again for this great video.
hey man , could you please do your change to code. obviously there is an issue with current one.whenever i move the target object and the curve is about to become a straight line the curve will be disappear and everything mess up ):
How would you go about using these equations to calculate the impact point of a thrown object? Every example I've seen predetermines the final position, but I want to know the landing point, given the initial velocity.
Steven then x=initv*time-0.5gt^2
Vector3 DisplacementZ = new Vector3(0f, 0f, Target.transform.position.z - Ball.transform.position.z);
Vector3 velocityZ = DisplacementZ/ (Mathf.Sqrt(-2 * height / Gravity) + Mathf.Sqrt(2 * (DisplacementY - height) / Gravity));
add these 2 lines of code and works in 3d, i mean when u move the object left and right
don't forget to change displacementXZ to displacementX and then set its Z coordinate to 0. then your code works great :D
Thank you so much for this, the AI couldn't just help me with creating an equation that would work, but this not only teach me a lot of stuff but helped me solve my problem, thanks a lot!
Thanks for this fantastic lesson, very useful for my game.
Math is magic!! thanks for this. I'm using it for a baseball game with players throwing the ball back and forth
i love this Equations series
Great video, I played around with it a bit. If I assume the ball is a soccer ball and add a reasonable drag coefficient(Cd) on the ball, say 0.25 - then the ball stops about half way. Is it possible to modify these equations to take into account the Cd ?
Does anybody know how I might be able to do this ?
(edit) The equations Sebastian provides is for 'high school' physics. When you add a drag coefficient onto your ball then the formulas don't work anymore. This is bc of air resistance (drag). There is an eq for air resistance but it is not a one time formula, you need to keep applying it per frame. Hence you cannot predict exactly where the ball will land. You can use integrals to get a fairly accurate path but in Unity it is not 100% accurate.
due thank you so much for making this. i was tryna implement somethig like this into my recroom game and i was having a brain aneurysm tring to put the equations in. Instant Like and sub
Saved me from a job challenge
Wow, this was really awesome. Thanks Sebastian!
All I can say is , its time to subscribe ..superb
i think i saw one video where you show some of the games you made through the years, and there was a game i would like to know how to make it, its like a mouse cursor dodger, where you need to try click a moving target on the screen, the target will move around and away from your mouse cursor on a precise way, dodging your cursors velocity maybe...? it didnt look like randomly moving cause that would obviously make the dodger fail by walking into your cursor x y position...
i didnt find it again, can you tell me about it?
i know we cannot control the speed of ball but is there any work around ? i am making football shoot game
Super Awesome tutorial thanks Sebastian . But Why this not works if target position is too high ?? How to solve this ??
Great work, very helpful. Thank you
How can I make flying object (let's say a missile) to look in the same direction as tangent to parabola ?
I'm looking for the same answer too
Thanks a lot man !
That helped me a lot in my Unreal project !
took me back to college days
I have a question, why did you use 18m/p^2 instead of 9.81m/s^2?
I prefer the solution + given information to all be integers for example problems.
I got you, my ocd was like hold up! XD Nice Video Btw!
+Ran Crump *ocpd
Probably because he wants to put that value. As a programmer you can make your own Physics engine and that includes switching the real values for values you want. If you write the code in the video and change the gravity default value for -9.81 you will have a good result using real values.
GRRRRRREEAAATT HELP! Thanks a lot Sebastian
The 12 dislikes are from people who had a positive gravity
TYSM! This was a very useful tutorial.
this was amazing please continue ....
For those want to change the velocity to get to the target, you can simply increase the gravity apply to the launch object.
I am using this method in my 2D project and I don't want to change the whole Physics2D setting. So I make Rigidbody2D.gravityScale = gravity(in this video) / Physics2D.gravity.y;
Does anyone know how I would be able to implement a projectile-motion-like approach to this ? I have a very similar situation, but with a cannon, so the cannon's rotation needs to influence both the x and y velocities. I have already tried implementing the cos(angle) for the x speed and sin(angle) for the y speed, but it didn't work 100%...
Physics.gravity affects all objects in the scene. Is there a esy to do this affecting the gravity of this single object?
Lovely tutorial. Except you didn't had to drive a formula for initial velocity for horizontal. You can directly use V = d/t and that's what your derivation ends up at.
Thank you, but is there any reason that checking debug is not showing draw path? Everything else works perfect good work !!!
i searched for something along the title, click, "hi everyone" IT'S HIM
Dude, you are fu*king awesome. Thanks a lot.
Hey,
I'm not sure if this is in the video but...
Is it possible to adjust the speed of the ball?
kind regards
I'm not sure whether you understood the purpose of this video. The ball's speed is actually being calculated in script so it always hits the target. Setting/adjusting the speed of the ball is easy.
i have the same question is there any work around ?
how can i increase the speed?
Hi, I found a solution to some extent. You can decrease the gravity value to -40 for a quick ball and increase it to -5 for a really slow ball. This is not the best solution, but it works as long as you don't change the values too much.
first i did it with timescale when launching ball .second s0lution is to increase the velocity on z axis but it will cause accuracy problem
Is it possible to change the speed of the moving projectile without causing it to leave it's arc?
Looking to create a script where a game object moves in a parabola from point A to point B , unlike this video where physics works, I just need it the game object to travel between those points, any help on that ?
if we consider some drag force in forward dierction than what will be the changes
It should also take into account the mass of the object. And do not start from the value of 1.
What difference do you expect the mass of the object to make?
I'm sorry. All formulas are mass independent - Trow ball by velocity, not by AddForce or else. My mistake. I checked your example on my project.
Now I can create a shooting mechanic for my basketball game!
Did it work?
Love you Seb!
Ive got a question, is it possible to calculate this if you know the horizontal motion and not the highest point?
so question. I wanted to get this equation to work in a unity project, but I am wanting to use it with a grenade, so I place an empty object in front of the player, and that is the endpoint and the player is the start point. I can not figure out how to convert it tho, any ideas?
really helpful. Thank you Sir
Thank you Sebastian
AWESOME, MAN!!! THANKS A LOT!!! Thats what i need!
Maybe someone solved this problem already but I cannot stick my head around it. I copied the code exactly as written in this tutorial but the center of my ball seems to miss the center of the target by about 0.5 on the x-axis. The calculated velocity is the same and I positioned all my gameobjects as in this scene... Anyone having an idea about this weird behaviour? I tried to watch this video in slow motion but I just cannot see wether the ball trajectory would hit the targets center given the colliders would be inactive.
I know this is an old video but how would you dynamically change the height of the arc based on the distance you are throwing it rather than have it as a set value?
I think you have to fix velocity or other parts to do that... In this case he fixed height.
is there a tutorial for this in gamemaker studio 2
This is the most thorough explanation of the subject I've seen so far. One question regarding the for loop when drawing the line: Is it a valid way for an AI to check if he can jump on a point (and not hitting the ceiling e.g.) or is there another way of doing it?
It is a good way, although obviously you can use a much lower resolution.
Hi Sebastian , I'm making a space exploration game in unity and the planet terrain has been stumping me for a while now and I was wondering if you could possibly help me at all or make a procedural planet series?
Question, are you done with the procedural terrain series?
+Sujisan4 I am not. I had planned to release the next episode end of nov, but looks like it will be early dec instead.
How to calculate g if h and t is given?
Great, awesome. Thank you very much
i think (velocityXZ + velocityY * -Mathf.Sign(gravity)) is equal with velocityXZ + velocityY. why need to times -Mathf.Sign(gravity) ?
This is amazing! Thank you so much!
Great video Sebastian, but what if you don't have the max height constraint and you still want to shoot an arrow in a curve between two points.
I think you'd need to pick a height, since an arrow can be shot in a curve between two points in a number of different angles, even in a straight line. If it were three points then you could use the middle one as the height
Why did you choose equation 4 for solving t? Wouldn't any of 1-3 work also?
how to calculate the wind (x-only), for this formula: VelocityX = DisplacementX / (Mathf.Sqrt (-2 * h / gravity.y) + Mathf.Sqrt (2 * (DisplacementY-h) /gravity.y)); ?
Who knows?
Could you do a tutorial on leading a target? Like for a sports game...
how can i make that trajectory visible after build?
How would I change the speed of the ball?
is there a way to manipulate the speed with this?
Great tutorial.
Hey Sebastian, awesome tutorial, I've been tinkering with this problem for a while now, you really helped me out, but I've one question, could you explain how I could apply this to a moving target?
Emiel van Essen i have a video on my channel on a turret that hits moving target 100% of the time
Also works in 3D
Can’t find link of the first episode.
Old but very nice tutorial, however, I can't figure out why you have taken the gravity as -18 m/s2 instead of -9,81 m/s2 as it is supposed to be. This deviated the simulation 1.4 times off of the reality.
Helpful video. But i have a question:
How can i decrease the time?
The rate of fall is a constant irregardless of the forward velocity of the ball. So to get the ball to hit a fixed target simply make the arc shallower. e.g if target is at 9, make the highest point of the arc, H, 9.0001
Great tutorial thank you
If the ball is missing the center of the hoop by 0.5 to 1 unit, this *might* help: I haven't watched the full video, but I asked about this for the first video (Mario jumping) on the Unity forum, and while I didn't understand the full explanation, apparently it has to do with Unity using semi-Euler approach to physics and not calculating the velocity based on the first physics frame but the second instead (or something like that). Try adding "... + (0.5 * Time.fixedDeltaTime * (whatever your acceleration is, the gravity) * -1) * Vector(dot)one" to the initial Rigidbody2D velocity and see if that helps.
Where did you get this formula from? I can't get the ball to hit the center with either Sebastian's original code or your addition
How would you do this with point gravity?
You really don't want to, believe me
Thank you!
Got so lost at the lets draw a line part 😂😢
mate could i change the speed the ball is travelling at ?
If you increase the gravity, the speed will be faster to compensate. If you decrease H (peak height) then the ball will be more direct and will have to go faster to counter gravity. But outside these constraints, going faster just means you miss the target in a purely physical simulation.
One option is that instead of a Rigid body you could use this math to create an animation and you can play that animation at any speed you like.
Thank youu. You are great ^^
Hello thank you for this video but how can we achieve this same problem using character controller.
Assuming you want to add also a curved trajectory horizontally instead of letting the ball fly horizontally a straight line. How would that work ? anyone ? oO
Great, great stuff
-18 m/s2 downwards is up
Thank you!!
my life saver :)
my god... copy paste... i love you