- Home /
Moving a GameObject along a Custom (Curved) Spline
Hi guys, After having extensively searched unityAnswers, the forum, the internet... I can't seem to find the solution to what I'm sure is a very simple problem.
I'm trying to make a 3rd person on-rails shooter. Really simple - a GameObject following a long spline that will curve at points, go straight at others, incline, decline etc. Like one can easily do in modelling software like Cinema 4D.
I found a few people that had asked this question, but never a conclusive answer. In my search I found the Spline Controller on the Unify Wiki (http://wiki.unity3d.com/index.php?title=Spline_Controller). But from what I can work out it only handles straight splines?
This must have a simple solution, they were doing this all the time in the 90s!! :) Thanks in advance to anyone willing to share some wisdom with me, really appreciate the help.
stop me if you've already heard this one, but have you investigated itween for unity? it has path movement and sounds like what you'd need
Thanks for the reply zombience , I did actually read about it on a forum but the person was dismissing it as being performance heavy and useless when it came to iOS so I didn't look into it any further. Do you have any experience using it?
Edit: I see that iTween is only for C# development? What a shame, it looked great.
FWIW
here's the simplest possible way i've ever been able to write routines to calculate equidistant points (actually approximately equidistant) and the tangents of those, along a cubic bezier:
float bezierPoint(float t, float a, float b, float c, float d)
{
float C1 = ( d - (3.0 * c) + (3.0 * b) - a );
float C2 = ( (3.0 * c) - (6.0 * b) + (3.0 * a) );
float C3 = ( (3.0 * b) - (3.0 * a) );
float C4 = ( a );
return ( C1*t*t*t + C2*t*t + C3*t + C4 );
}
float bezierTangent(float t, float a, float b, float c, float d)
{
float C1 = ( d - (3.0 * c) + (3.0 * b) - a );
float C2 = ( (3.0 * c) - (6.0 * b) + (3.0 * a) );
float C3 = ( (3.0 * b) - (3.0 * a) );
float C4 = ( a );
return ( ( 3.0 * C1 * t* t ) + ( 2.0 * C2 * t ) + C3 );
}
The four values, C1 C2 C3 C4, are sometimes called the coefficients of the bezier. (Obviously, you could just separately calculate those one-time if you feel like it.)
The factors a b c d are usually called the four "control points" for a bez.
Of course, t runs from 0 to 1, say every 0.05. "t" is conceptually like time as you move along the bezier.
If you are working in two dimensions, simply call the whole thing with all the X values to get the X result, and separately, with all the Y values for the Y result. In three or more dimensions again simply call the whole thing once for each dimension.
I am working on a project with the same concept as described above, yet I want the player to be able to move the object/camera forward and backward along the path . $$anonymous$$any of the solutions posted here seem to be more suitable for an animated camera flythrough. I'd be very happy if someone has a hint on this.
Answer by Graham-Dunnett · Apr 30, 2013 at 09:15 PM
http://wiki.unity3d.com/index.php/Hermite_Spline_Controller
The screen shot on this page shows curved splines, so I don't think it's straight splines only. Alternative, visit Google and enter:
site:u3d.as spline
and hit the search button. You'll get loads of solutions. Perhaps your extensive search was faulty? ;-)
Well I have to admit I am a little embarrassed as Hermite looks like a great solution. I blame my n00by lack of confidence :-) so from one Graeme to another (Graham), thanks so much.
that's intriguing about the "u3d.as" idiom ... does that mean the site is "registered" in some way with google, that google knows that abbreviation? Good one
Hey fattie, that's something I can help you understand - I'm an SEO :-)
site: is a search operator that filters your search so that Google only returns results for your given keyword that are hosted on the given website. As Graham illustrated the syntax is like so: site:domain.com keyword So yes u3d.as, the unity asset store, is "registered" with Google in a way: The pages hosted on the domain have been crawled and indexed by Google, and therefor show up in search results.
Hope this helps!
SEO = Search Engine Optimizer (save you having to look it up if, like me, you didn't know :)
Answer by Fattie · Jan 22, 2015 at 09:48 AM
hey David if you're still looking at this,
the absolutely perfect solution is
https://www.assetstore.unity3d.com/#!/content/2110
there are a number of competing packages on the asset store. we bought them all and that's the best one.
"This must have a simple solution" ... by no means. it takes expert programming in a number of fields and mathematics. we have lots of code base for mesh extrusion, splines etc but it's the sort of thing where it's much. much more intelligent to just use an existing proven package.
with that package (or indeed the competitor packages), the job is done. hope it helps
Hi. I'm looking at that extension. Do you know how to use it with root animation? For example if I have a rolling forward animation, how can I make my model following a spline while rolling but still keep the root animation? Thank you!
Answer by Koyemsi · Mar 05, 2018 at 12:04 AM
I've just tried a free asset called Surge that does the job. It has many cool features, not only splines but also tweens and others I haven't tried yet. https://assetstore.unity.com/packages/tools/utilities/surge-107312
Your answer
Follow this Question
Related Questions
How to follow a moving object by camera on a line ? 1 Answer
object on a spline following another object(superspline) 0 Answers
Camera rotation around player while following. 6 Answers
how to continue spline? 0 Answers
How to generate railings (roller coaster track) along the created spline? 0 Answers