- Home /
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?
Thanks
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.)
Answer by mike002 · Mar 02, 2010 at 02:34 PM
You might be able to use something like spline controller from the UnifyCommunity wiki,
What a pitty... I get this warning:
You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed. $$anonymous$$onoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.$$anonymous$$onoBehaviour:.ctor() UnityEngine.$$anonymous$$onoBehaviour:.ctor() SplineInterpolator:.ctor() (at Assets\AI\SplineController\SplineInterpolator.js:1) SplineController:OnDrawGizmos() (at Assets\AI\SplineController\SplineController.js:24) ...
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.
Answer by Jaap Kreijkamp · Mar 02, 2010 at 01:55 PM
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.
Thanks for your answer, I will look into it. But Ani$$anonymous$$ate gives an error when trying to rotate ("rotation") so I don't know if I can use this.
Answer by Phil 5 · Mar 03, 2010 at 01:18 PM
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 :)
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.
do you want straight line only? or a merge between straight and smooth corner?
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.
Answer by Charles-Van-Norman · Oct 27, 2010 at 06:47 PM
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
Answer by The_r0nin · Oct 28, 2010 at 12:55 AM
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.
Your answer
Follow this Question
Related Questions
Advice on implementing metroidvania camera 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Camera follow help? 1 Answer
Smooth Follow Rotation 3 Answers
Camera following two objects 0 Answers