• 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 BobbleHead · Jun 30, 2011 at 10:14 AM · camera2d

2D moving camera Camera (scrolling from a-b)

I've searched the answers and google, but couldn't quite find the answer, so apologies if this has already been covered

In games such as classic shoot 'em ups, (those does by Cave/Capcom etc) the player is followed by the camera, yet the camera is continuously moving along a set axis. I know by attaching a camera to the player, it'll follow regardless, but I wish for the camera to scroll continuously, whilst the player is confined within the view..

How would I go about doing that?

 transform.Translate(Vector3(0,speed,0) * Time.deltaTime);

I know the above line will scroll the camera in the axis I wish, however that's if the camera Is a child of the Player, so when I move player camera moves aswell... which sounds great but the camera adds the playerspeed to it's own speen I think, so it doesn't work well.

Any ideas?

EDIT: I unparented.. that worked... now really just confining the player within the cameras view

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
0

Answer by Waz · Jun 30, 2011 at 11:46 AM

Convert your player position to Viewport space (X and Y are a number from 0 at left/bottom of screen to 1 at right/ top, Z is distance from camera):

 var vp = Camera.main.WorldToViewportPoint(player.transform.position);

then clamp it to the screen:

 vp.y = Mathf.Clamp01(vp.y);

(same for X and you may want to clamp it with a margin)

then convert back to world space:

 player.transform.position = Camera.main.ViewportToWorldPoint(vp);

do this after you have processed player movement controls (or as part of it).

Comment
Add comment · Show 5 · 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 BobbleHead · Jun 30, 2011 at 01:25 PM 0
Share

sadly that didn't make sense to me, and unity threw up an error about player being an unknown identifier. Which didn't make any sense.

I think the above is rather complicated and may take a while for me to understand, thank you for your comment though.Could you recommend any script references to explain the above?

Thank you

avatar image Waz · Jun 30, 2011 at 02:25 PM 0
Share

player is your player. If the script is itself on the player, you don't need player.transform, just use transform.

avatar image BobbleHead · Jun 30, 2011 at 03:27 PM 0
Share

sadly i get the folowing error:

NullReferenceException UnityEngine.Camera.WorldToViewportPoint (Vector3 position) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/Graphics.cs:638) playerScript.Update () (at Assets/playerScript.js:30)

avatar image BobbleHead · Jun 30, 2011 at 10:27 PM 0
Share

function Update () {

     var x$$anonymous$$ove : float = Input.GetAxis("Horizontal") * Time.deltaTime * 2;
     transform.Translate( x$$anonymous$$ove , 0 , 0 );
      
     var z$$anonymous$$ove : float = Input.GetAxis("Vertical") * Time.deltaTime * 2;
     transform.Translate( 0 , 0 , z$$anonymous$$ove );
     
     var vp = Camera.main.WorldToViewportPoint(transform.position);
     vp.z = $$anonymous$$athf.Clamp01(vp.z);
     vp.x = $$anonymous$$athf.Clamp01(vp.x);
     transform.position = Camera.main.ViewportToWorldPoint(vp);

my code is above, where am I going wrong? :( still throwing up error

avatar image Waz · Jul 01, 2011 at 05:34 AM 1
Share

That error means There is no Camera.main, so no camera tagged $$anonymous$$ainCamera in your scene.

avatar image
0

Answer by Waz · Jul 01, 2011 at 12:28 PM

See also : http://answers.unity3d.com/questions/62189/detect-edge-of-screen.html

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Resizing orthographic camera to fit 2d sprite on screen 1 Answer

Orthographic Static 2D Background - IOS/Iphone 3,4 & 5 1 Answer

Camerascript works strange! 0 Answers

Camera 2d following player with delay jumps 1 Answer

Movement object same distance as touch movement 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