• 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 siege911 · Feb 17, 2014 at 02:03 AM · camerafollowclamp

Camera follow object when it reaches edge

I'm struggling to figure out how to actually implement what I'm wanting to do and was wondering if anyone could help.

I have an Orthoscopic Camera and a Rigidbody. The Rigidbody will get launched to the right (x-axis positive) direction. While it's possible to launch it to the left, there will be an invisible collider that basically stops it from moving left beyond the camera. On the y-axis, I don't care if it goes above the camera and the ground prevents it from going down.

So I want the camera to follow it in the positive x-axis direction ONLY. To make things a little more complicated, I'd like it to move with the object once it gets almost to the edge of the screen. Think like 20% left before it gets to the edge. Once the object continues in the positive x-axis position, I want the camera to keep up with it so it never goes outside the camera.

I'm thinking along the lines of mathf.clamp but I'm still unsure how to implement it so maybe I'm way off.

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
1
Best Answer

Answer by Wuzseen · Feb 17, 2014 at 02:10 AM

This a problem that is semi-common and there are a couple solutions I've seen.

The one I'm partial to the most is defining dead zones in terms of screen coordinates. That is to say, you prevent the camera from panning while your character is within, say, 20% and 80% of the X coordinates of your screen. When the character goes past 80 or 20 then the camera moves to follow the character based on delta movement along the X axis. You can duplicate this to do the same thing on the Y axis. So your camera doesn't move in world space while your character's position on screen space is within your dead zone for camera movement.

As far as not allowing the camera to go to the left past a point, just do a check if the camera's X world position is to the left of the point you want it to stop at. Likewise in preventing the camera moving down, add a check to keep it above.

You can do that with Clamp or just a simple conditional. if(camera.position.y < val) camera.position.y = val; kind of logic.

Comment
Add comment · Show 1 · 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 siege911 · Feb 17, 2014 at 03:12 AM 0
Share

Awesome, thanks. Based off of this, it helped get my wheels turning and I came up with the following script (attached to $$anonymous$$ain Camera within Update function) and it works great!

 /////////////$$anonymous$$ove camera with ball
     //We will only move the camera if the ball is flying
     if (ballFlying) {
         //Set the edge boundary to check the ball against
         var edgeBoundary : Vector3 = camera.ScreenToWorldPoint (Vector3 (Screen.width * .8,camera.transform.position.y,ballTransform.position.z));
         //Check the camera position against the golf ball to see if it passed the boundary
         if (ballTransform.position.x > edgeBoundary.x) {
             //$$anonymous$$ove camera the difference
             camera.transform.position.x += ballTransform.position.x - edgeBoundary.x;
         }    //End ball position check
     }    //End if ball flying

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

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

Related Questions

Camera follow smoothness problem 1 Answer

camera follow an game object 0 Answers

2d camera follow player by looking ahead. 1 Answer

I can't figure out how to clamp it 2 Answers

Rotate camera up and down with mouse input with to min and max 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