- Home /
how to follow curved path?
If I want an object to travel from point A to point B, but also want the object to start from facing its own forward direction to facing aligned with a direction B.forward, then what is the best way to implement this? So far I calculate a bezier curve with my own script, but I cannot find the length of the curve which I need so that the object can travel at a constant speed. Some solutions talk about evaluating the curve numerically but this seems inefficient for many moving objects.
Example this would be a way a car moves from point A to point B.
simply get "SuperSplinePro" from the asset store. it does everything and everyone uses it for everything. its as essential as a text editor.
Answer by demented_hedgehog · Jan 23, 2015 at 01:24 PM
iTween does what you want I believe (available for free in the asset store).
Edit... apparently Super Splines Pro is very good be sure to look at that as well.
true but SSP is just .. well there's no point not having it, it does it all and pretty well
Fattie has a lot more experience about this stuff than I do. So make sure you check out SSP here: Super Splines Pro
Answer by aldonaletto · Oct 07, 2013 at 01:17 AM
If you really want to follow some curve, maybe this whydoidoit's article may help you. On the other hand, if you just want to move in the object's forward direction while turning gradually to the target, do exactly this: calculate each frame the target direction, Slerp a little to it and then move the object in its current forward direction:
public var speed: float = 5.0; // linear speed in m/s
public var turnSpeed: float = 1.5; // rotation speed control
var target: Vector3; // assign the target position to this variable
function Update(){
// update direction each frame:
var dir: Vector3 = target - transform.position;
// calculate desired rotation:
var rot: Quaternion = Quaternion.LookRotation(dir);
// Slerp to it over time:
transform.rotation = Quaternion.Slerp(transform.rotation, rot, turnSpeed * Time.deltaTime);
// move in the current forward direction at specified speed:
transform.Translate(Vector3(0, 0, speed * Time.deltaTime));
}
This almost works, but I arrive at the point (which is actually a transform) not facing in its forward direction. So i guess my example is more like driving a car into a garage or parking space.
Yes, the object keeps looking towards the target, not to the target's forward direction - doing this will require a curved path evaluation, for sure: if the target is facing the previous target, for instance, a curve around the target must be created so that the object enters it in the correct direction.
Old thread but the idea may help someone... Previous comment from 'aldonaletto' plus adding an empty object really far (infinite!) in the target's forward direction and using a Transform.LookAt on this one.
Answer by Fattie · Oct 07, 2013 at 11:44 AM
note that mathematically, points on bezier curves over equal T are approximately equidistant only.
But it is utterly inconceivable that it would matter in a video game.
What you normally do is simply look at (literally use "LookAt" in unity) a number of points ahead (say, 4 points ahead -- try different values -- 3, 2, 5 etc -- for the best look).
This is precisely how any race game you've ever played works.
Again it's utterly impossible to know what you're trying to do .. unless you tell us, even in the most general terms. What is the domain of speed etc? (Is this a 2D arcade game? a parking simulator? a race game? flight game in 3D? or?)
I am making a hamster game, I have hamsters that are first roa$$anonymous$$g around the bottom of a cage and then climb up some tubes. It looks funny if they walk to the enter point, pivot on a point, and then jump in. The bezier curve works with but the hamsters do not hold constant speed. I have been looking into the maths and they are much more complicated than I thought.
Actually what I am really having a hard time finding is curvature at any point, so I can update my hamster to walkLeft or walkRight animation. The only thing that works at the moment is calculating the tangent of two circles that defined the maximum turn radius of the hamster. Then the hamster follows the circle, goes straight, goes around a second circle and is there.
you simply need SSP. it would be insane to program it from scratch. what you describe would literally take 20, 30 seconds with SSP.
it would be like if you "did not use" unity's UI and for some reason got in to program$$anonymous$$g buttons on your own, and handling image display and stuff. SSP is the only solution
Answer by 0x73DB07 · Sep 26, 2014 at 08:02 PM
FWIW, I've begun using Curvy (http://www.fluffyunderware.com/pages/unity-plugins/curvy.php), and if your application depends heavily on splines, I would say it's worth the money. No relation to the developer, just a user.
Your answer
Follow this Question
Related Questions
How to generate a curve from a model ? 2 Answers
Access curves presets from c# 1 Answer
2D simple road system with curves and intersections 0 Answers
moving forward after bazier curve 1 Answer