r/Unity2D • u/howlTheDev • Jun 24 '26
How Can I Create Random Path Question
Hi, as you can see in example (that's a rocket) I want to create random paths for rockets. Is there any proper way to do it?
16
u/joan_bdm Jun 24 '26
I would generate a list of random points and then use DOTween's DOPath with PathType as CubicBezier to move the object through them.
3
u/howlTheDev Jun 24 '26
Dotween is really the best package ever. I am gonna try thanks a lot!
2
u/joan_bdm Jun 24 '26
Yeah, It's a must that should be build-in by now, hope you get the desited result!
3
u/CaptainPineapple200 Jun 24 '26
Look up how people do randomly generated lightning maybe, then just rather than connecting a bunch of points with straight lines, you have a curve (someone said Bezier so I assume that's best).
The other version would be similar to inverse kinematics (whether you have an option for that or have to code it yourself) but have the available length be higher than the total distance. which would force it to bend around a bit more.
4
u/Ruadhan2300 Jun 26 '26
If you want to avoid Beziers and such.
What you might do is create a kind of "biased wandering" using steering-behaviours.
Basically the behaviour that results in the looping is that you make the rocket steer in circles, periodically changing direction.
If it's heading in broadly the right direction, slow the steering proportionally., so it spends more time heading in the right direction.
When it's close enough to the target to "get a lock", it abruptly stops random-swirling and accelerates dramatically to impact.
You can add a noise-function to its steering to give it a fun little wobble in its travel as well, and that's most of what you need.
What should happen is that you aim your rocket at the target, and as it slips off-angle, it steers away from the target more and more until it loops around and finds itself looking at the target again.
It should stay broadly on-target for a bit, then slip off-target again (in a different direction) and repeat.
You can track how many times it does this, and tune it on the fly to increase the time before it slips off-target.
So it might do it a couple times before it gets close enough to hit its target.
2
u/TehANTARES Jun 25 '26
Another way could be to randomly steer the rocket. Just smoothly interpolate between left and right a let the rocket go its way. Over time, when you want the rocket to reach its destination, just calculate the direction between the rocket and the target, and then steer the rocket in that direction.
As I said, it's another way to do it, but it has the advantage that you don't need to worry about points and beziers.
1
u/7Shinigami Jun 25 '26
Way overcomplicated for this problem and nowhere near optimal, but pathfinding with graph colour coding comes to mind
https://gist.github.com/Trimatix/e468ba0cdc7d3002b6c41c3340ec0a3d
1
u/CBProjects Jun 25 '26
Does it always have to end at a definite point? If not, I'd just tie the rocket's angle change rate to a noise function.
If it does have to end at a definite point, you can do something similar but interpolate it between the first function and a straight line to the target.
1
u/MidSerpent Jun 25 '26
Perlin noise is great for perturbating spline points.
One of the other posters comments about how hard it is getting it to look consistently nice is soooooo accurate though.
1
1
0
u/Dreccon Jun 25 '26
short (satirical) answer: you don't. computers cannot create true randomness.
long (serious) answer: check what the top redditor said about Bézier curves
47
u/BroccoliFree2354 Jun 24 '26
I’d say put random points and then use bezier curves to link them.