Flythrough example using Bezier's and waypoints

Hi all,

I’ve been playing around with creating a flythrough for the title screen of my game and thought I would share it. I’m sure there are other examples out there but I couldn’t find exactly what I wanted so I did it myself. Maybe this will be helpful to someone.

The flythrough consists of a empty game objects, one of which has the FlythroughController script attached and all the others (waypoints) have the FlythroughWaypoint script attached.

Using Bezier curve things the result is a nice smooth path through the waypoints. Using Gizmos the waypoints and resultant path are visible in the Scene view and can be modified visually.

The math is done in MyBezier.cs. I originally didn’t realise what I needed and implemented a N-point Bezier system, but it only uses four points at a time.

The FlythroughController is the object that moves, so for the example project here I just added the SmoothFollow script to the camera and make the flythrough controller the target.

For extra bling, try running the Game with Gizmos on. Looks like a rollercoaster :slight_smile:

192999–6867–$flythroughexample_189.unitypackage (207 KB)

Would you mind checking the file please, I tried to d/l it, but it came up all cryptic.

Thanks

-Raiden

looks very usefull

Thank you for contributing

This is very cool. Thanks for sharing!

@Raiden – it appears to be showing the unity package (binary) as a text file. If you save it, and then import the package into Unity, it works just fine.

Very Useful Package.
Thanks.

I needed this. Thank you.

Cheers, hope you can get some use out of it.

One thing that needs fixing is that if you add another waypoint it is added to the end of the list of waypoints (in the hierarchy). That order is the order that the script pulls them out using GetComponentsInChildren().

I’ve tried to drag and drop the waypoints into different positions in the Hierarchy but it seems to do nothing. Is there a way to reorder items in the hierarchy?

Thanks for sharing! I have not tried it yet, but it might be exactly what I was going to need next week ;).

About your hierarchy: I think the documentation says not to rely on that order, because it might not be always correctly retrieved using any of the enumeration options. Or possibly I am confused with the actual Start() function being called.

Anyway, what about using an public array variable which you have to fill with the objects you want to use? And array order is the fly order then. Just a random thought…

Re: Almar

Yeah that would probably work well.

It would still have the problem of wanting to insert a waypoint in the middle of the others, because you would have to manually shift all the others down wouldn’t you? But I think that is a problem common to all publicly (inspector) editable arrays. It would be nice if you could click on an element of the array and choose insert or something.

Hi thanks for a great script. All is working fine, except for when I enable the controller script after startup, the controller
starts at random positions along the curve.If I enable everything at startup,everything is fine. Any ideas on what is causing this?

If you look at the script, in the Update() method there is this

float currentTime = Time.time % SecondsForFullLoop;

Time.time is the time since the game started.

If you make a member called ElapsedTime or something and initialise it to zero, then put in update…

ElapsedTime += time.deltaTime;
float currentTime = ElapsedTime % SecondsForFullLoop;

it should then only start running when it’s enabled (ie when Update is called).

Awsome!!Thanks very much,it solved the problem! :smile: :slight_smile: :idea:

This is extremely useful for so many applications.

thanks!

Hi Sam,

Do you know a way of easierly allowing for variable speed.

The bezier path is calculated at initialization based on the SecondsForFullLoop value.

I want to be able to control the speed of my camera at any point as it moves around the full path.

Any help greatly appreciated, and thanks again for a great bit of code!

Do you know a way of easierly allowing for variable speed.

I’ve been working on this spline code, here’s my second attempt at cleaning it up.

dt in SplineController determines where on the curve your next position will be (0.0 start and 1.0 the next-to-last-point).

227307–8187–$test2_988.zip (1.19 MB)

Wow, thanks Troy!

I have downloaded and had a play with it.

I added the following code, after the dt += Time.deltaTime; line.

		if (Input.GetAxis("Mouse ScrollWheel") > 0){ 
			path_time = path_time - 1.0f;
			if (path_time < 0.0f ) {
				path_time = 0.0f;
			}
	}
		
		if (Input.GetAxis("Mouse ScrollWheel") < 0){ 
			path_time = path_time + 1.0f;
			if (path_time > 20.0f) {
				path_time = 20.0f;
			}
		}

And it enables me to change the speed of the camera as it moves around the waypoints, but it is very jerky.

Any ideas how to make it nice and smooth?

Thanks again!

Greg,

Don’t use a set value like 1. You don’t know how often this script is being called so 1, will be added at different times - thus the jitter.

Instead of:

path_time = path_time + 1.0f;

try:

path_time += Time.deltaTime;

if this is not enough (Scroll speed is too slow) then add a multiplier (value becomes larger but still dependent on time elapsed since last time script was called.

path_time += Time.deltaTime * 10;

Thank you very much ,this is what I am looking for.

But when I use the code in Unity For iPhone 1.5.1 ,it seems that there are some error like this :

I have modified my code as follows:

path_time = path_time + Time.deltaTime * 20;

And it almost works as desired, the jerkiness is much less, but it still exists, and when I scroll the mouse wheel the camera jerks back slightly, before adjusting its speed around the waypoints.

What I am trying to do is have a camera or game object follow a preset waypoint path smoothly, with the ability to adjust the speed of the camera or object.

If anyone can help me get this working, I would be so grateful!

Greg,

What I used (and it worked quite nice if I remember - don’t need it anymore however) is some code I found where a group of people were trying to emulate the controls from WoW.

Link is here:

http://forum.unity3d.com/viewtopic.php?t=18073&highlight=wow+camera

might be a valuable read.

Remember to post a solution if you find one. I will take a look at my code if I can find it and post it here as I think my scroll code worked fine (but it was based on the WoW code so read that too).