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 ChaosX2 · Jan 31, 2014 at 12:17 AM · camera2dboundsborderscroll view

How to stop view from scrolling when edge of view collides with a game object?

Hello, I am coding a game with two players and I am having an issue figuring out how to stop the camera from scrolling when a player walks too far from the other player. The way I have it set up is that once a player touches a certain point near the edge of the view, it clamps the players position (both left and right side of view) so that the object doesn't go off screen.

However, if I keep moving my player one, the view continues to follow player one and drags player 2 along for the ride against the other edge of the view. Instead of this happening, I want the view to not allow player 1 to continue if the other is against the view border.

So far, I have this is the code I am using for what I have thus far.

 Vector3 viewPos; //Variable to convert world space to view port space 
 Vector3 delta; //Change in camera position relative to player1 position
 Vector3 destination; //Variable to hold camera's change in position to keep players in view
 
 //Variables to hold a value to measure the borders on screen to contain players
 float leftBorder = cam.ViewportToWorldPoint(new Vector3(0.06f,0,dist)).x;
 float rightBorder = cam.ViewportToWorldPoint(new Vector3(0.95f,0,dist)).x;
 
 //Clamps the players position onto the camera view
 player1.position = new Vector3(Mathf.Clamp(player1.position.x,leftBorder,rightBorder),player1.position.y,0);
 player2.position = new Vector3(Mathf.Clamp(player2.position.x,leftBorder,rightBorder),player2.position.y,0);
 
 if(player1)
 {
 viewPos = cam.WorldToViewportPoint(player1.position);
 delta = player1.position - cam.ViewportToWorldPoint (new Vector3(0.5f,0.5f,viewPos.z));
 destination = transform.position + delta;
 destination.y = 0;
 transform.position = Vector3.SmoothDamp(transform.position,destination, ref velocity, dampTime);
 }

Before I decided to ask for help, I was trying to find a solution right before the last line of code by creating an if statement to check if the players.x position (player1.position.x) is greater than the view then perhaps stop the view from scrolling, but it wasn't working for me and I'm not sure how to halt the position of the camera.

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

0 Replies

· Add your reply
  • Sort: 

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I get the bounds in PolygonCollider2D? 0 Answers

Keep player from falling outside the camera? [2d] 1 Answer

CustomCameraController: Rotating my camera breaks my out of bounds code 0 Answers

How to Calculate the border of Camera in Specific Depth? 0 Answers

2D camera rotating relatively to game object speed 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