Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by theCompanionStar · Jun 11, 2012 at 01:46 AM · cameratrigger2d-platformer

2D Style Camera, Shifting at Screen Edge

Hey, I'm pretty new to scripting and Unity but I'll try my best to convey my issue.

My question, if you wish to help:

In a 2D-style platformer, how can the camera stay fixed on a "room" until the character reaches a side of the screen and walks off, prompting the camera to shift to the section he's entering?

I think I could start off with either:

  1. Triggerwalls in each room that shift camera over

  2. Calculate screen edges in real world space, if player exits shift that way

  3. Set empty object in center of room, raycast to player

Any thoughts?

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

1 Reply

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

Answer by Mortoc · Jun 11, 2012 at 02:53 AM

I think the easiest way would be to have a script keep track of the player's position in view space since it sounds like you'll wanna tweak this behavior.

 void Update() {
     Vector3 viewPosition = camera. WorldToViewportPoint(player.transform.position);
 
     if( viewPosition.x > 0.9f ) // move right
     else if( viewPosition.x < 0.1f ) // move left

     // etc
 }
Comment
Add comment · Show 2 · 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 theCompanionStar · Jun 13, 2012 at 08:36 PM 0
Share

That sounds great! I'll have to try it when I get home from class. I'm guessing vertical would be similar but with viewPosition.y, but I'm wondering how to set up camera movement after that. Would I have a smooth movement of the camera in the world equal to or at least related to view width or height?

avatar image theCompanionStar · Jun 14, 2012 at 11:35 PM 0
Share

I got the detection on the sides working, but I'm trying to set up a translate of the camera in the direction. I figured that something like Camera.ViewportToWorldPoint could be used to calculate the screen width and height in world units and set the result as the distance in my transform.Translate, but it's not moving that distance. Any other suggestions on a reliable tracking of viewport width for the translation?

EDIT: Actually, I got it working, generally. Just had to fix the z in ViewportToWorldPoint

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to destroy a projectile when it leaves any side of the camera view on a 2D game? 1 Answer

Cinemachine Confiner 2D shift my camera view 0 Answers

How to make the camera move up and down the horizontal axis only when the 2D character get's above and below the top half of the screen. 0 Answers

Array for multiple cameras? 2 Answers

Camera switching by trigger 2 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