Hit a Moving Target - Godot 3D Tutorial - Trajectory and Aim - Code and Math

  Рет қаралды 2,109

Neal Holtschulte

Neal Holtschulte

Күн бұрын

#gamedev #godot #godot4
00:35 The Code
03:01 The Math
11:02 The Scene
13:32 Q&A
15:19 Bugs
I'll show you how to calculate the point to shoot at in order to hit a moving target. I am not using hitscan weapons. No raycasts are used. All projectiles move through space over time.
This works in 2D and 3D. I'll demonstrate using Godot 4.2 in 3d and I will explain the math. There's also a surprisingly detailed discussion of our choice of bullet scene root node and how to apply decals.
Link to github page of the Godot project:
github.com/nea...
Link to a zip file of the Godot project:
drive.google.c...
Free assets used are available here:
kenney.nl/asse...
and here:
www.freepngimg...
For the bullet hole decals I used moeDev's "How to Create Improved Decal/Bullet holes In Godot 4." tutorial:
• How to Create Improved...
Dot Product Properties:
en.wikipedia.o...

Пікірлер: 20
@NealHoltschulte
@NealHoltschulte Ай бұрын
All code is available on github: github.com/nealholt/TargetLeadExample This follow-up vid explains the off center shots and adds compensation for bullet drop: kzbin.info/www/bejne/fqvKoGiAp5eaf5Y
@errantoverflow4828
@errantoverflow4828 3 ай бұрын
Thanks, I was trying to wrap my head around the maths behind this and you did a spectacular job.
@fedmakesgames
@fedmakesgames 6 ай бұрын
Thank you!!! This video has clarified the math for me. I am following a udemy course that gets the math wrong and magically reaches the correct result. Thank you very much
@dannyeisenga
@dannyeisenga 13 күн бұрын
Thanks so much, this really helped me :) The case in which the bullet is slower than the target can be optimized a bit, though. Because `time` in that case is just 0, you end up multiplying the target velocity by 0, and then adding that to the target position. You can can simply return the target position instead, and you can even do the check before calculating a, b, and c. You can also check the squared bullet speed against the square length of the target velocity vector to skip the square root operation. You need the squared bullet speed later anyway, for calculating a. In my case I also added a check at the top of the method to simply return the target position when its velocity is approximately zero. Anyway, thanks again :) I first tried implementing a much longer function that I found on a gamedev website but I couldn't make that work. While I understand the principle I can't say I understand the math, but at least I have something working now :)
@NealHoltschulte
@NealHoltschulte 12 күн бұрын
Thanks!
@Unity_and_Peace
@Unity_and_Peace 6 ай бұрын
What a Time This video was uploaded I thought it was uploaded 3 or 4 years Back🤣 BTW. This Help me a alot for my Project Thanks !
@NealHoltschulte
@NealHoltschulte 7 ай бұрын
It seems I didn't try very hard to answer my question about Area3D bullets versus CharacterBody3D bullets. I have since learned that CharacterBody3D WILL give better accuracy at some computational overhead cost. Area3Ds are simplest, but at speed may lose accuracy or pass through targets. Raycasts, though not relevant to this video, are simple and efficient, but not "realistic" in that they are instantaneous.
@dennissolomon497
@dennissolomon497 Ай бұрын
Hey Neal, great video! What modifications to the mathematical formula would be needed to take into account bullet drop / gravity? Perhaps in the form of a float value, or a vec3
@NealHoltschulte
@NealHoltschulte Ай бұрын
Great question. Off the top of my head, the speed x time right side of the equation probably needs replaced with the horizontal component of projectile motion V0 x time x cos angle. I’m not sure what else. Traveling right now but I’m excited to answer this question when I get back.
@dennissolomon497
@dennissolomon497 Ай бұрын
@@NealHoltschulte Alright I appreciate your time, thanks in advance!
@NealHoltschulte
@NealHoltschulte Ай бұрын
It was harder than I expected, but I did it! The most up to date code is on github currently. Video here: kzbin.info/www/bejne/fqvKoGiAp5eaf5Y
@kritik_mb2144
@kritik_mb2144 6 ай бұрын
I wish the title would include keywords like trajectory/aim-prediction so, its easier to search and find this video.
@NealHoltschulte
@NealHoltschulte 6 ай бұрын
That is super easy to fix. Thanks for the suggestion.
@arizun8160
@arizun8160 7 ай бұрын
Great video. Vectors are really cool
@nickvlahos4547
@nickvlahos4547 7 ай бұрын
AYO, this is cool.
@OnyxIdol
@OnyxIdol 5 ай бұрын
I have tried three methods of this so far and none are working correctly, still trying to find my problem (the intersect isn't far enough in the front). As for the collision, I use a Raycast3d in my bullet scene, the root of which is just a Node3d. On _physics_process I evaluate the collision of the raycast. That at least seems to be working just fine. I tried using a Characterbody3d, but that just ended up pushing my targets around lol.
@NealHoltschulte
@NealHoltschulte 5 ай бұрын
If you use a raycast, then you don't need my video because it tells you where the hit occurred. If the characterbody3d pushes the target around, that's likely a masks versus layers issue. You may need to assess whether the bullet needs to know about the target or vice versa, but not both. Good luck!
@OnyxIdol
@OnyxIdol 5 ай бұрын
@@NealHoltschulte I'm not using a Raycast like a hitscan. Rather, a Raycast3D of limited length moves with the projectile and it used to see if the projectile in its forward motion collides with something. And thanks :)
@NealHoltschulte
@NealHoltschulte 5 ай бұрын
@@OnyxIdol Oh, I understand. Hmm, I thought the character bodies took care to interpolate the positions inbetween the start and end of a frame so that they don't "tunnel" through something they should have hit, (in which case you wouldn't need a limited length raycast carried along with the projectile) but I gotta admit, you've moved into an area that I don't know much about.
Every 3D Node In Godot Explained In 20 Minutes
21:17
Ryan Games
Рет қаралды 8 М.
Incredible Dog Rescues Kittens from Bus - Inspiring Story #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 40 МЛН
3D Space Combat - Godot - Code available on github!
42:54
Neal Holtschulte
Рет қаралды 150
How I Made a 3D Platformer in 2D Game Engine
21:23
ggenije
Рет қаралды 487 М.
The Most Impressive Scratch Projects
11:00
DenshiVideo
Рет қаралды 4,9 МЛН
The biggest lie in video games
15:18
AIA
Рет қаралды 1,9 МЛН
Matlab: Fractals (and complex numbers)
27:21
Neal Holtschulte
Рет қаралды 812
A Brief Overview of the World Environment in Godot 4
9:52
Watt Interactive
Рет қаралды 7 М.
Why Stairs Suck in Games... and why they don't have to
11:24
Nick Maltbie
Рет қаралды 1,5 МЛН
Should you learn C++ in 2023?
8:06
Dreams of Code
Рет қаралды 55 М.