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 /
  • Help Room /
avatar image
1
Question by Threadlight · Jul 31, 2016 at 03:02 PM · camera2d game2d-platformercamera-movementcamera follow

How to stop the camera when the player has reached the edge of the level?

I have viewed some tutorials and they do work but it's a bit finicky and the camera does not re-center on the player when respawned.

I'm making a 2D platformer game where the camera is centered on the player and follows the player around. However, I don't want to make the camera see past the edges of the level but I still want the player to be able to move around and touch the edge. Also, the camera should follow the player around again once the camera is centered on the player again. Can someone please explain how I can go about this? Thanks.

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 Dibartolomeo · Jul 31, 2016 at 03:22 PM

Im not sure, but try to use a simple operation with Mathf.Clamp combined with the Update().

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
0

Answer by SebasSBM · Jul 31, 2016 at 06:14 PM

I would try to fix such kind of issue by limiting the camera move using a Rect, or rather better, a Collider2D set as "isTrigger" that covers the whole level and defines their bounds (or better said, bounds that cam shouldn't cross).

So, with the "trigger collider" defined, you can attach this to the cam's script to control events when the cam goes out of the "trigger collider" (ALERT: The snippet is in Javascript. You might need to convert it's syntax to C# if you are using C#):

 function OnTriggerExit2D(other : Collider2D) {
     // I assume your cam has the tag "MainCamera" attached
     if (other.tag == "MainCamera") {
         /*  Whatever logic you might need to implement in order to limit the camera's moves.
          *  You might need to do coord checks to see if cam went out of bounds by right,
          *  left, up and down, and limit it in a different way depending on this
          */
     }
 }

I strongly reccommend to read carefully my snippet rather than copy-pasting blindly. Is much more like a template rather than a complete snippet of code. I would complete it, but due to I diidn't see your code, I can only guess how you made it.

It might also be helpful for you to see the features of OnTriggerExit2D method, as well as OnTriggerEnter2D and OnTriggerStay2D (all them methods of the MonoBehaviour class), as well as the features of Collider2D class.

And, in general, you can find complete documentation about any class or method used by unity in docs.unity3d.com

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 Threadlight · Aug 01, 2016 at 11:38 AM 0
Share

Ok, thanks for your help! I deleted the code attached to the camera but I can give you the code which I found in a tutorial which I based my own script off, modifying it slightly to work with the game of course. It worked fine but the only problem was the issue of re-centering the camera on the player.

From here: http://answers.unity3d.com/questions/600429/stop-movement-at-edge-of-screen-2d-shooter.html

  // X axis
  if (transform.position.x <= -4.3f) {
      transform.position = new Vector2(-4.3f, transform.position.y);
  } else if (transform.position.x >= 4.3f) {
      transform.position = new Vector2(4.3f, transform.position.y);
  }
  
  // Y axis
  if (transform.position.y <= -2.7f) {
      transform.position = new Vector2(transform.position.x, -2.7f);
  } else if (transform.position.y >= 2.7f) {
      transform.position = new Vector2(transform.position.x, 2.7f);
  }

 
avatar image SebasSBM Threadlight · Aug 02, 2016 at 06:03 PM 0
Share

It would work. The inconvenient of this approach is that it uses hardcoded values, which is a bad practice.

Below I post the same code, but without hardcoded values (Javascript). This way the code is more "human-readable" and it becomes easier to change the values.

 // Values with a meaningful variable name
 var leftBound : float = -4.3f;
 var rightBound : float = 4.3f;
 var upBound : float = 2.7f;
 var downBound : float = -2.7f;
 
 // X axis
 if (transform.position.x <= leftBound) {
     transform.position = new Vector2(leftBound, transform.position.y);
 } else if (transform.position.x >= rightBound) {
     transform.position = new Vector2(rightBound, transform.position.y);
 }
 
 // Y axis
 if (transform.position.y <= downBound) {
     transform.position = new Vector2(transform.position.x, downBound);
 } else if (transform.position.y >= upBound) {
     transform.position = new Vector2(transform.position.x, upBound);
 }

It might be good for you to learn more about variables and functions in Unity.

PD: About the "re-centering the camera" issue, it would be hard to resolve using this approach. It would probably be easier re-centering the camera using the "trigger collider approach"

avatar image SebasSBM Threadlight · Aug 02, 2016 at 09:00 PM 0
Share

@Threadlight There's also another problem with your question: I don't know how you implemented camera following. "Did you made the cam a child of the player Gameobject? Or do you move the camera programmatically?" Even though I am willing to help you, without knowing this information it is hard to give you a good answer.

I invite you to read this website's user guide and FAQ section to obtain tips about how to make good questions. This way, users willing to help like me will have it easier to help you. You can edit this question to make it better, just click on the $$anonymous$$achine Cog logo next to the question title, and click Edit.

One more thing: adding Javascript or C# tags to your question will let other users know which program$$anonymous$$g language you are using, avoiding unnecessary misunderstandings.

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

6 People are following this question.

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

Related Questions

Camera not following 2D character when the character reaches the ground. 0 Answers

Why is my camera shaking when I use my chaser script on something? 0 Answers

Does anyone know the script for a smooth camera follow of the main game object? 8 Answers

When attaching my custom camera script camera shakes when player starts to move fast. 0 Answers

I build a own game but i have a cam problem 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