Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by Henk Jan Baard · Mar 02, 2010 at 01:50 PM · camerafollow

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.)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

7 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by mike002 · Mar 02, 2010 at 02:34 PM

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eurosat7 · Mar 02, 2010 at 04:00 PM 0
Share

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) ...

avatar image Henk Jan Baard · Mar 04, 2010 at 07:06 AM 0
Share

Thanks mike002 for helping me out!

avatar image Henk Jan Baard · Mar 04, 2010 at 09:39 AM 0
Share

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.

avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Henk Jan Baard · Mar 03, 2010 at 09:22 AM 0
Share

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.

avatar image
1
Best Answer

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 :)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Henk Jan Baard · Mar 04, 2010 at 07:06 AM 0
Share

I will try this one also. Thanks!

avatar image Henk Jan Baard · Mar 04, 2010 at 10:57 AM 0
Share

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.

avatar image Phil 5 · Mar 04, 2010 at 12:55 PM 0
Share

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

avatar image Henk Jan Baard · Mar 05, 2010 at 07:29 AM 0
Share

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.

avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges