r/howdidtheycodeit • u/ScaryImpact97 • Jan 06 '23
Question How did they code homing attacks? In sonic games, all i found were 2D tutorials
27
Upvotes
r/howdidtheycodeit • u/ScaryImpact97 • Jan 06 '23
43
u/tranceorphen Jan 06 '23
You need some way to get the closest target, so some kind of spherecast.
Once you have a list of potentials, you perform simple 3D math to find the closest target.
Then you apply your new movement towards the target's position, as your proximity check will return that info so you can calculate distance.
If you add in damping you can decide how quickly you 'home' in on a target.
All of this can be abstracted and encapsulated in many different ways.
3
u/Gibgezr Jan 06 '23 edited Jan 07 '23
Just to add to what everyone else is saying, here's some simple, direct and un-optimized C++ code showing how you might restrict an entity's turn rate (this is taken from a working program, and there's some conversion between degrees and radians in there because of the graphics engine that was used):
5
u/joshuaism Jan 06 '23
Is it not just vector math? velocity = velocity + normalized vector pointed at target's current coordinates?
2
u/Tom_Bombadil_Ret Jan 06 '23
Unity has a built in function called Lerp (Linear Interpolation) which can transition a variable from one vale to another smoothly. It’s a common method for homing attacks and works on both 2D and 3D vectors. would recommend doing some research as to how to recreate that in your engine/language of choice. It may not be exactly what you are looking for but it’s a start.
1
u/NUTTA_BUSTAH Jan 07 '23
Math is same in 2D and 3D.
You need to get more specific than that because that is all there is to it.