Hello everyone!
I’m trying to create a projectile trajectory that I can edit using a curve or something similar to determine how it would travel from point A to point B.
Most of the resources I found only discuss arcs, but I’d like to create other types of trajectory paths.
I made an illustration to better explain this, showing three examples.
Is there a way to achieve this? Maybe by using an editable curve to make the trajectory behave as intended?
In 2D you can use an AnimationCurve where the 0 to 1 range is mapped to the current distance from source to target. AnimationCurve provides you with a custom Inspector to draw the curve.
In 3D you’d have to use an AnimationCurve for each axis, at least two, and it’ll be less intuitive to edit but still doable.
Thanks @CodeSmile !
Could you post a simple script example on the 3D implementation?
Just use Unity Splines. They have examples for objects following splines. You can scale the result to hit the target (so that the end point of the spline is right on your enemy).
I have also experimented with other spline solutions such as Catmull Rom where it automatically connects between Points - but the result was pretty much the same (just that it worked better with runtime modifications of those points to add some randomness and limit trajectories and such).
Thank you, everyone!
I guess this will be trickier than I imagined. I’ll need to do some serious research on it.
Thanks again!
Don’t overcomplicate it.
At first, wrap the good old Vector3.Lerp()
method in your own method that accepts start point, end point and a fraction from 0.0 to 1.0.
Now as your projectile travels, compute the fraction and call the above method.
With that, the bullet will go straight to target.
Now with version 2, look at what @John_Leorid suggests above and make a different function that wraps a spline evaluator and accepts the same three arguments, and swap it out, perhaps conditionally.
OR… use something like a Mathf.Sin()
curve to simulate a snaky path to destination, offsetting it laterally between beginning and ending points.
It’s nice to build game software in little iteratively simple steps because you learn as you go, and you may soon realize that this isn’t quite the direction you want, and you realize it sooner by getting something working, anything working, as soon as possible.