• 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
0
Question by Nelis · Mar 03, 2013 at 12:49 PM · camerajavascriptmovementfollowfixed

Fixed camera on rails.

Hello, I'm trying to find out how I can make a proper camera script. My camera needs to follow an object up, down, left and right. So it can only move on the X and Y axis. I also want to make it so there is a bit of feathering to it. I have looked through Unity Reference but I can't find it. Thank you! -Nelis

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

2 Replies

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

Answer by Khada · Mar 03, 2013 at 12:58 PM

You have to code t$$anonymous$$s sort of t$$anonymous$$ng manually and there are endless techniques that can be used to ac$$anonymous$$eve varying results. Here's how to get the camera to follow an object on the X and Y axes to get you started.

 //set t$$anonymous$$s to the object to follow
 public GameObject followObject;
 
 //I recommend moving the camera in the LateUpdate method
 void LateUpdate()
 {
     Vector3 camPos = Camera.main.transform.position;
     camPos.x = followObject.transform.position.x;
     camPos.y = followObject.transform.position.y;
     Camera.main.transform.position = camPos;
 }

EDIT: Attempted java version (I don't use java, there may be mistakes):

 //set t$$anonymous$$s to the object to follow
 var followObject : GameObject;
 
 //I recommend moving the camera in the LateUpdate method
 function LateUpdate()
 {
     var camPos : Vector3 = Camera.main.transform.position;
     camPos.x = followObject.transform.position.x;
     camPos.y = followObject.transform.position.y;
     Camera.main.transform.position = camPos;
 }
Comment
Add comment · Show 14 · 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 Nelis · Mar 03, 2013 at 01:06 PM 0
Share

Thank you, but can you write this in javascript please?

avatar image Khada · Mar 03, 2013 at 01:19 PM 2
Share

There you go (hopefully it's correct).

avatar image Nelis · Mar 03, 2013 at 02:33 PM 0
Share

I'm sorry, but it doesn't work. The camera doesn't move. I'm also getting a 'NullReferenceException: Object reference not set to an instance of an object'

avatar image Khada · Mar 03, 2013 at 03:00 PM 0
Share

As per the comment, you have to set 'followObject' to the object you want the camera to follow. You can do this via the inspector. It won't work unless you do this.

avatar image Nelis · Mar 03, 2013 at 03:28 PM 0
Share

I have done that.

Show more comments
avatar image
0

Answer by makegoalken · Mar 04, 2020 at 08:13 AM

Thank you very much for these great cake recipes, I have learned a lot from your web blog candy crush soda

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera Scripting Help (Sims Style) 0 Answers

Moving Question 1 Answer

Character Runs Off Camera 2 Answers

Camera follow Avatar 1 Answer

Making my look script turn smoothly 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