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 negative_zero · Nov 22, 2014 at 11:44 PM · cameracamera-movementcamera rotate

Unity 2D... getting accurate X and Y values of camera when rotating it.

Hey all! I am currently working on a 2D runner and I am using my camera position to determine when to spawn objects. At the beginning of each stage I make a list of every object that will be spawned and its spawn position, and then when the player gets close (basically, when the objects are just off of the player's screen), the object will spawn. So far I have been using this code to get my camera position:

 camera_location = Camera.main.ViewportToWorldPoint(new Vector3(1, 0.5f, Camera.main.nearClipPlane));

I then use a simple comparison on the x position in Update to create a list of stuff that will spawn that frame:

 short_spawnerpositionlist = spawnerpositionlist.Where(x => x.x_position - distance_to_spawn < camera_location.x + x.distance_to_spawn_x_offset).ToList();

And voila. It works. I use something similar to despawn once off the screen.

HOWEVER. I recently added an object which, when the player hits it, makes the camera spin around once on its z-axis (so basically the screen spins clockwise in one full circle.) Here is the code:

 if(is_rotating)
         {
             transform.Rotate(0, 0, Time.deltaTime * rotate_smooth);
 
             if(rotation_last > transform.rotation.eulerAngles.z)
             { 
                 is_rotating = false;
                 rotation_last = 0;
                 transform.rotation = Quaternion.identity;
             } 
 
             rotation_last = transform.rotation.eulerAngles.z;
         }

I noticed this totally messes up the spawns and despawns while the spin is happening. Looking into it a bit, I realized that the way I am getting camera location each frame is affected by the rotation of the camera... so my x value, for instance, will go from 2 all the way to like... -6 or something... and then back to 2 even though the camera / player has not moved at all, just the camera's rotation.

So obviously I need another solution. I'm thinking maybe there is some way to get camera_location that is more about getting the location in fixed space, such that even if you rotate the camera it is still giving the fixed x location? Or something? Does anyone have any ideas here?

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
0

Answer by DavidMann · Nov 23, 2014 at 01:38 AM

The problem you're having is that you're making measurements through the viewport. When the camera moves the world point it is pointing at moves. Just use Camera.main.position. All you're doing by projecting to the nearClipPlane is adding a small Vector3 depending on the magnitude of your nearClipPlane. So, if necessary, just add that Vector to your camera's position. With a rtation of (0,0,0) and a nearClipPlane of .3, you'd add Vector3(0,0,.3) to your camera to attain the same thing but independent of rotation.

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 negative_zero · Nov 23, 2014 at 05:30 AM 0
Share

I think the reason I did this is that I wanted to be able to get the exact right side of the screen no matter what the player's resolution. Is there a way to do this with the regular Camera.main.position?

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

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

Related Questions

How do I make this Camera movement happen when I right click? 1 Answer

need help with roll a ball like game 0 Answers

How to set horizon level for camera orbit script? 0 Answers

How can i make the camera look up and down through keyboard keys? 2 Answers

Stop camera from stuttering when looking straight up or down 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