• 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 · Sep 06, 2011 at 06:21 PM · camerabounds

Camera border

I'm trying to make an Android top-down shooter game (spaceship only moves on X and Y axis) for my first Unity project and I'm running into difficulty. The camera will be fixed and I want the player spaceship to be within the screen. How would I create a barrier around the screen?

I was looking at Camera.WorldtoScreenPoint but in testing it seems like it's more the physical camera bounds which may exceed the screen bounds. I used the code found here and it said I hadn't reached 0 until I was well off the screen.

I also can't do a static barrier because Android screen sizes vary.

I'm trying to find some way to relate an XY position on the screen (Z position will always be exactly the same distance from camera) to an XY position in space. I also need this for things like boss fights where I want the boss to go to specific areas on a screen (like in a circle around the screen while the player has to dodge). How would I do this?

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 Waz · Sep 06, 2011 at 06:41 PM 0
Share

Camera.WorldToScreenPoint is the function to use. Show your actualy code, because you're doing something wrong.

avatar image siege911 · Sep 06, 2011 at 06:52 PM 0
Share

Currently the only thing I'm doing to try to figure this out is this sample code:

 var target : Transform;
 
 function Update () {
     var screenPos : Vector3 = camera.WorldToScreenPoint (target.position);
     print ("target is " + screenPos.x + " pixels from the left");
 }

When I move my spaceship to the end of the screen, it doesn't display 0. For instance, right now I have my spaceship on the left-most part of the screen and it says "target is 300.9166 pixels from the left". Then if I grab the X axis in the Scene view and move him while the game is running and drag him over, the number doesn't change but then use the keyboard (the current input for moving him in game) to move him in the game view it changes. For instance, now that I've done that, and move him back to the edge of the screen, it says "target is -165.9049 pixels from the left".

I would think that camera.WorldToScreenPoint would be a static position. Basically it looks at the character compared to his position on the screen rather but it seems to be a bit more relative but I'm not sure exactly to what.

avatar image Waz · Sep 06, 2011 at 07:20 PM 0
Share

Sounds like your not dragging the right object. Is your spaceship composed of multiple objects, with children? Have you dragged the correct object to the target var? When you moved it in the scene view, did you accidentally drag a child of target rather than target itself?

avatar image siege911 · Sep 06, 2011 at 07:59 PM 0
Share

Ha. You were right. I was dragging a child object. Now it's making more sense.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cj_coimbra · Sep 06, 2011 at 06:58 PM

Is it a 2D top-down? If so just make everything 2D textures and work with viewport space interval.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

3D object space occupied in camera 0 Answers

Find camera bounds at certain distance from camera? 1 Answer

Ease in transform.LookAt() - keep other target within camera boundaries 1 Answer

How can I get the bounds in PolygonCollider2D? 0 Answers

WebGL camera doesn't go past screen bounds 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