• 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
Question by SamTheT · Mar 02, 2015 at 04:41 PM · camerajavascriptmovement

Moving Camera only when player crosses a point

I was thinking about making a platformer, but I don't want the camera to move with player, I only want it to move, when player crosses the screen border, so you kinda get that feeling that you move between levels even though you are in the same location (examples : Zelda, Pokemon).

I tried to do this myself, wrote a small script, but my problem is that I still do not understand how to use Transform, transform and (G)(g)ameObject stuff.

Here's the code itself

 #pragma strict
 
     var player : Transform;
     var cam : GameObject;
 
     function Update () {
 
         if (player.position.x > Screen.width) {
         
             cam.transform.position.x += Screen.width;
 
         }
         if (player.position.x < -Screen.width) {
         
             cam.transform.position.x -= Screen.width;
 
         }
 
     }

Please help guys, that's the only problem I've got so far

Comment

People who like this

0 Show 0
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

Answer by andylatham · Mar 02, 2015 at 07:56 PM

What you're doing in principle works, but you're just not updating the camera position properly. You can't assign a value to it like any other variable, you have to assign a vector to it.

So instead of writing this:

 cam.transform.position.x += Screen.width;

write this:

 cam.transform.position = new Vector2 (cam.transform.position.x += Screen.width, cam.transform.position.y);

Depending on what you're doing, you may need Vector3 rather than Vector2, in which case you'd need to specify the z-position too. There may be better ways to do it, but that's the way that I know! :)

Comment

People who like this

0 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 NoseKills · Mar 02, 2015 at 07:56 PM 0
Share

You forgot the "equals" in there

  cam.transform.position = new Vector2 (cam.transform.position.x + Screen.width, cam.transform.position.y);
 

Another pesky problem you have to tackle is that Screen.width is in pixels (and depends on the screen size of a mobile device for example), whereas your character is moving in world units. You can get the screen size in world units with this method. Then just use that width instead of Screen.width (just the first lines of code in the answer)

avatar image SamTheT · Mar 02, 2015 at 10:08 PM 0
Share

Okay, so, I combined all the stuff on this topic, that you guys wrote with my script and it works! the only problem now is that it works too much, it constantly goes left and right, will try and fix

avatar image SamTheT · Mar 02, 2015 at 10:12 PM 0
Share

and yeah, it doesn't draw a thing, but everything else works right

avatar image

Answer by hexagonius · Mar 02, 2015 at 05:36 PM

Transform.position is in units and Screen.width is in pixels. Also Screen.width does not have a position somewhere.

You need to transform the right screen edge into world space:

 if (player.position.x > Camera.main.ScreenToWorldPoint(new Vector3(Screen.width,0,0)).x) {
 Vector3 newPos = Camera.main.transform.position;
 newPos.x += Camera.ScreenToWorldPoint(new Vector3(Screen.width,0,0)).x;
 Camera.main.transform.position = newPos;
 }
Comment

People who like this

0 Show 7 · 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 SamTheT · Mar 02, 2015 at 05:46 PM 0
Share

thanks for the answer! now unity says, that I need to insert a semicolon after Camera.main when defining Vector3 I tried to define Camera.main to cam, but that didn't work

avatar image hexagonius · Mar 02, 2015 at 05:56 PM 0
Share

My bad, I updated the code

avatar image SamTheT · Mar 02, 2015 at 06:22 PM 0
Share

don't see any changes...

avatar image hexagonius · Mar 02, 2015 at 06:30 PM 0
Share

I changed the parameters for Camera.ScreenToWorldPoint from float to Vector3.

avatar image SamTheT · Mar 02, 2015 at 06:32 PM 0
Share

oh, right, sorry

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Fixed camera on rails. 2 Answers

Making my look script turn smoothly 0 Answers

Move RigidBody character relative to camera. 2 Answers

Camera Script Amalgamation == Camera Script Abomination, Im In Over My Head 0 Answers

Camera movement(I have no clue how to code) 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