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 Dr_GeoFry · Sep 21, 2013 at 04:00 AM · cameragameobjectdistance

Keeping a Camera focused on the GameObject in a 2D Game

HI everybody,

I am revisiting a game I created a while ago. My question is this: How do I make the camera stay focused on the central perspective as the action moves to the right? right now, I have a scenario where the left edge of the screen will kill the player if it touches him. It isn't focused on him because if it was, it would move when he jumped, which would expose parts of the scene I don't want the user to see. The problem is that if he moves to far to the right, he can go past the visible threshold...

It seems like a simple enough solution, and here is what I am thinking. You calculate the distance between the player and the gamObject, in this case, the Camera. When the distance exceeds whatever appropriate distance, then the gamObject increases its distance back to a specified distance "Home" distance.

it would look a little bit like this:

 var distance; 
 var target : Transform;
 var cameraTransform : Transform;
 var initialDistance = 3.0;    
 
 function Awake() 
 {     
     cameraTransform = transform;     
 }    
 
 function Start()
 {
     target = GameObject.FindWithTag("NAME").transform;
 }
 
 function Update() 
 {     
 var distance = (target.position - cameraTransform.position).magnitude;
 }

then a bunch of if statements etc. describing the when to add the distance from current distance.

Does that make sense? Am I setting that up correctly in your eyes??

Thanks for your help everybody.

Comment
Add comment · Show 4
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 getyour411 · Sep 21, 2013 at 04:11 AM 0
Share

As the player, I'd probably find it annoying if the camera lagged behind me then zoomed back in with me as the focus only to lag behind me again as I moved to the right. Would it work well in your game if you used the player as your focus but ignored up/down movements on the camera?

avatar image Dr_GeoFry · Sep 21, 2013 at 04:23 AM 0
Share

@getyour411 it is essentially a simple side scrolling game I worked on a long time ago. It doesn't zoom. It should just push the right edge further in front of the player. my goal is to have the player not go past the right edge of the screen. Currently, the player just goes beyond the right edge and is not seen.

avatar image robertbu · Sep 21, 2013 at 05:11 AM 1
Share

What you describe does not make much sense to me. Not sure of your game mechanic, but I one idea is to convert the player's position into a viewport coordinate (Camera.WorldToViewportPoint()). Viewport coordinates go from (0,0) in the lower left to (1,1) in the upper right. So as the player passes beyond the middle of the screen (Viewport x coordinate of 0.5), you would increase the scroll speed of the camera. I'd likely scale it higher depending on how close to the edge (x coordinate of 1.0), and perhaps smooth the increase.

avatar image Dr_GeoFry · Sep 21, 2013 at 05:51 AM 0
Share

thanks, @robertbu. I am not extremely knowledgable of all aspects of code. This sounds like a great solution to me. Thanks for pointing me in a new direction.

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

16 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

Related Questions

Distance gameobject to camera edge 1 Answer

Camera to follow a target within a circle? 1 Answer

Gameobject to stay within the view of the camera - c# 3 Answers

Calculate Distance Between two Game objects 1 Answer

Camera rotation the same as player rotation 1 Answer


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