Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Kobting · Aug 21, 2014 at 12:15 PM · camerapixel

How to spawn an object inside the camera's view

The title is pretty hard to understand so I'll go into more detail of what I'm trying to do. So what I want is for the object to spawn randomly within the camera's view. The problem I'm having is that I can't figure out how to set the area the vector can be at inside the camera based on the pixels because I need those to offset the object to make sure it's not half way in the camera's view. (right now I'm not offsetting for the object size because I haven't created them yet, I'm just using circles and cubes)

Here is what I have and if you could explain my problem that would be great:

     public Vector2 randomVector2()
     {
         float camWidth = Camera.main.pixelWidth;
         float camHeight = Camera.main.pixelHeight;
         float camX = Camera.main.WorldToScreenPoint (Camera.main.transform.position).x;
         float camY = Camera.main.WorldToScreenPoint (Camera.main.transform.position).y;
 
         Debug.Log ("Cam X " + camX);
         Debug.Log ("Cam Y " + camY);
 
         float x = Random.Range (camX - (camWidth / 2), camX + (camWidth / 2));
         float y = Random.Range (camY - (camHeight / 2), camX + (camWidth / 2));
 
         return new Vector2(x, y);
     }

 
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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by robertbu · Aug 21, 2014 at 04:15 PM

How this problem is solved, as well as how easy or hard this problem is to solve, is based on a number of factors:

  • Is the camera orthographic or perspective?

  • Are the object all on a plane parallel to the camera plane or at random distances from the camera?

  • Is it important to snug objects to the screen edges, or is a little slop okay?

  • Do the objects vary in size or are the similar in size?

If the objects are similar in size, on a plane parallel to the camera plane, and you can live with a bit of slop, you can solve the problem using Viewport coordinates. Viewport coordinates go from (0,0) in the lower left of the screen to (1,1) in the upper right. The code:

 float x = Random.Range(0.05f, 0.95f);
 float y = Random.Range(0.05f, 0.95f);
 Vector3 pos = new Vector3(x, y, 10.0f);
 pos = Camera.main.ViewportToWorldPoint(pos);

This code creates a point 10 units from the camera plane and it will be 5% away from all the edges.

Comment
Add comment · Show 2 · 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 Kobting · Aug 22, 2014 at 09:02 AM 0
Share

Thanks, that was exactly what I was trying to achieve.

avatar image onlyashley04 · Apr 26 at 05:44 PM 0
Share

Thanks! I've been trying to figure out how to generate prefabs randomly within perspective camera view for hours.

avatar image
0

Answer by Alessio89 · Aug 21, 2014 at 12:28 PM

You should be able to achieve that using Camera.pixelHeight and Camera.pixelWidth. These two are read-only properties of the Camera class and they return the size in pixels of the viewport. You can then get a random value between 0 and pixelWidth and convert that to worldpoint (or better use a raycast and spawn the object on the hit point, but that's another topic).

I've never done this myself, so I might be wrong here. But that's how I would try it. (Can't do that now sadly)

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

Answer by TripathiPushkar · Jan 05, 2021 at 10:36 AM

If my camera is not parallel to the game object then how to calculate the z axis . My camera is at angle

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

Answer by Delgoodie · Jan 05, 2021 at 01:19 PM

@Kobting Pick a random z distance from the camera, then get the transform.forward of the camera, create max height and width variables and use pythag to find their values:
height = transform.forward.magnitude / Mathf.Cos(verticalFOV / 2)
width = transform.forward.magnitude / Mathf.Cos(verticalFOV / 2)

Make sure to be in correct degrees / radians! Then pick a random value x, y value between the bounds and create a position vector that is:
tranform.position + transform.forward * randomZ + transform.up * randomY + transform.right * randomX

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get pixeldata from graphics.drawmesh? 0 Answers

Restricting ratio of a 2D Game 2 Answers

Questions about Unity 2D camera setup 1 Answer

Pixel Perfect Camera auto *zoom/scales* on different resolution 1 Answer

How to make camera position relative to a specific target. 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