How to let a GameObject or Camera follow a path?

I want to let a vehicle or camera follow a path (stick to a line) that I defined in design time. (Like a train following a rail). The vehicle or camera would be operated by the user (press a key to go forward or another key to go backwards).

I really don't have any clue how to build something like this, and I can't find some tips to do something like this.

Can someone help me with this? Or can you give me some tips?


Update: All solutions given here use splines which gives the wrong effect for me. I want to move, say a car, along a path in a straight line and at some point turn into a corner (the path would be straight followed by a quarter of a circle, followed by a straight line again.)

more ▼

asked Mar 02, 2010 at 01:50 PM

avatar image

Henk Jan Baard
509 16 23 50

(comments are locked)
10|3000 characters needed characters left

7 answers: sort voted first

You might be able to use something like spline controller from the UnifyCommunity wiki,

more ▼

answered Mar 02, 2010 at 02:34 PM

avatar image

66 1 3 3

What a pitty... I get this warning:

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor() UnityEngine.MonoBehaviour:.ctor() SplineInterpolator:.ctor() (at Assets\AI\SplineController\SplineInterpolator.js:1) SplineController:OnDrawGizmos() (at Assets\AI\SplineController\SplineController.js:24) ...

Mar 02, 2010 at 04:00 PM eurosat7

Thanks mike002 for helping me out!

Mar 04, 2010 at 07:06 AM Henk Jan Baard

I tried this one but it's not what I'm looking for. There are 2 orientation modes: NODE and TANGENT. With NODE the camera keeps facing in the same direction also when the spline is turning; With TANGENT the camera is rotation constantly is the direction the spline is turning on the next turn. So if you have a straight line that will arc later, the camera is already turning in the arc direction. And this is also not what I want. I'd like the camera to turn the moment the spline turns. So I have to find a different solution.

Mar 04, 2010 at 09:39 AM Henk Jan Baard
(comments are locked)
10|3000 characters needed characters left

There are numerous options, but if you want to keep coding to absolute minimum, you could try to make an animation of a point in your 3D program, and in Unity attach the camera to the animated point and play the animation. Another option that doesn't require too much programming is AniMate.

more ▼

answered Mar 02, 2010 at 01:55 PM

avatar image

Jaap Kreijkamp
7.3k 46 71 114

Thanks for your answer, I will look into it. But AniMate gives an error when trying to rotate ("rotation") so I don't know if I can use this.

Mar 03, 2010 at 09:22 AM Henk Jan Baard
(comments are locked)
10|3000 characters needed characters left

I think this website have the script you are looking for: (but it is in french) http://www.unity3d-france.com/unity/?p=105

Google translated link: http://translate.google.com/translate?js=y&prev=_t&hl=fr&ie=UTF-8&layout=1&eotf=1&u=http%3A%2F%2Fwww.unity3d-france.com%2Funity%2F%3Fp%3D105&sl=fr&tl=en

I hope it will help you :)

more ▼

answered Mar 03, 2010 at 01:18 PM

avatar image

Phil 5
80 7 6 13

I will try this one also. Thanks!

Mar 04, 2010 at 07:06 AM Henk Jan Baard

These solutions all use splines so you can't create straight lines that at some point make a curve like a car turn a corner. So also this solution is not what I'm looking for.

Mar 04, 2010 at 10:57 AM Henk Jan Baard

do you want straight line only? or a merge between straight and smooth corner?

Mar 04, 2010 at 12:55 PM Phil 5

I want a straight line followed by a quarter circle line and then a straight line again. With splines you get lines that are smooth flowing. But if you want to simulate a person in a vehicle you don't want smooth flowing lines.

Mar 05, 2010 at 07:29 AM Henk Jan Baard
(comments are locked)
10|3000 characters needed characters left

Here is a camera following a line created with Blender (or 3D equivalent.) It is not controllable but this may be the "base" script that you need. Full project file included. http://forum.unity3d.com/threads/5438-Camera-path-technique

more ▼

answered Oct 27, 2010 at 06:47 PM

avatar image

Charles Van Norman
157 4 5 7

(comments are locked)
10|3000 characters needed characters left

For the line, you simply need to point the camera in the correct direction (use the equation of a line) and Translate(0,0,X). For the quarter circle, use the equation of a circle. What you want is a circle (x^2 + z^2 = r^2) that is not centered on the origin (your camera's starting location), but has the position of the camera on the circle itself at (0,0) with the circle's center at (-r,0), ignoring the y for the moment. (which will then follow 1/4 of the circle until it has made its turn). So basically you want to calculate your x position as a function of z:

if r = radius of circle, and z goes from 0 to r ... one-quarter of a circle, the distance of the radius from the origin... then you can plot the position as:

x = (r^2 - z^2) - r

That will plot out a quarter circle path for you. Then you simply need to position your camera at (x, constant, z) for each z from 0 to r.

To change the direction of the camera, use theta = z/r*90, then Rotate from the original angle (0, theta, 0).

This will allow you know the exact position and angle of the camera at every step along your path and quarter circle. Just remember to store your original coordinates (before the start of the turn) and reset to those each time before you position and rotate.

more ▼

answered Oct 28, 2010 at 12:55 AM

avatar image

1.6k 28 27 50

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Mar 02, 2010 at 01:50 PM

Seen: 45506 times

Last Updated: Feb 09, 2014 at 03:33 PM