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 dchen05 · Jan 17, 2013 at 05:51 AM · camerarandomrayintersect

Choose a random point that is within view of camera

Hi guys.

I've seen many questions that ask how to check weather or not something is visible, but I am now wondering how to choose a random point within the bounds of the camera.

I have a standard rectagular plane, but the camera is angled so only a cone shape is in view.

  var screenY = Random.Range(10,1120);
  var screenX = Random.Range(10,630);
  var ray : Ray = Camera.main.ScreenPointToRay(Vector3(screenX,screenY,100));
  Debug.DrawRay (ray.origin, ray.direction * 400, Color.yellow);  


Currently my plan is to use ScreenPointToRay from the camera and then try and figure out where the ray intersects my plane, and that will give me my point. However that path seems a bit like a roundabout way of doing things, is there any other ways to do this?

Comment
Add comment · Show 1
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 Lovrenc · Jan 17, 2013 at 05:53 AM 0
Share

I did this once, but i dont know wheather that is the best way. So:

I cast ray from all corners of game and than saved the world coordinate values i got hitting the plane. I used those values to then randomly position objects on my plane.

This works if your camera is not moving. Also on resize i guess you have to redo the process.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by CodeMasterMike · Jan 17, 2013 at 07:15 AM

Little pseudo code, but maybe something like this would work?

 float screenX = Random.Range(0.0f, camera.main.pixelWidth);
 float screenY = Random.Range(0.0f, camera.main.pixelHeight);
 float ScreenZ = Random.Range(camera.main.nearClipPlane, camera.main.farClipPlane);
 Vector3 point = camera.main.ScreenToWorldPoint(new Vector3(screenX, screenY, ScreenZ));
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 dchen05 · Jan 17, 2013 at 01:38 PM 0
Share

This almost works. The problem is the Z axis here. I want the points to show up on a flat plane. farClipPlane and nearClip plane both give points that are parallel to the camera. Since my camera is angeld against my plane, the Z distance is longer at the top and shorter at the bottom. I tried manually setting the Z to what I needed right after this code, but then sometimes I get points that are off camera.

avatar image CodeMasterMike · Jan 18, 2013 at 06:40 PM 0
Share

[EDIT] Damn, I missed that you angle the camera, and not the plane :-P But I leave my previous text here, in case it might give you an idea how to do it.

Hmm.. Then first, you get the x and y axis as above. Then with the z-axis, you would need to calculate little more. So if you take the boundingbox (Rect) from the plane, and with the x and y axis you should be able to figure out what the z-axis should be.

Say the boundingbox goes from (100, 100, 25) to (0, 0, 0), and the random numbers gives you (25, 25, z).

Then you can calculate where z is on the boundingbox with the help of the y-axis. Since the higher up you are on the y-axis the closer you get to 25, and the lower you are on the y-axis the closer you get to 0.

And so you should be able to get the correct z-axis, which in this case would be 6.25 (25, 25, 6.25).

The math: 25/100 = 0.25. 0.25 * 25 = 6.25

In text: RandomYaxis / $$anonymous$$axYaxis = Procentage. Procentage * $$anonymous$$axZaxis = zValue.

You should be able to get the right z-axis using a Lerp calculation. I haven't tried it, but I think it, or something similar to it, would work. It might be worth a try :-)

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

10 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

Related Questions

randomised camera "recoil" 1 Answer

C# Randomize Background.Color Issues 1 Answer

How can I detect non-rectangular bounds? 0 Answers

Offseting a Ray from camera before casting 1 Answer

How can I make a camera shake more smoothly (Random.insideUnitSphere)? 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