Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ut_tommy · May 18, 2016 at 02:11 PM · cameratransformtransform.positiontransform.rotate

How to spawn objects on a restricted sphere surface relative to camera?

Hello everyone,

I'm trying to spawn targets at random positions relative to my camera with certain restrictions, however I'm having plenty of trouble getting the transformations right.

How can I spawn an object at some distance D from my camera with

  1. A random horizontal angle offset from my current orientation between [-hA, hA] where a horizontal offset would correspond to moving the mouse left/right.

  2. A random vertical angle between [-vA, vA] where vertical angle would correspond to moving the mouse up/down.

Thanks, Tom.

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
1
Best Answer

Answer by elenzil · May 18, 2016 at 05:43 PM

look up spherical-to-cartesian conversion. it's pretty straight forward.

lmk if you can't find something that works for you.

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 ut_tommy · May 19, 2016 at 12:02 PM 1
Share

Thanks elenzil, that helped me arrive at working solution.

This is what I came up with:

     private Vector3 generateTargetPosition() {
         float currentPhi = 180 - (centerEulerRotationRange(Camera.main.transform.localRotation.eulerAngles.x) + 90);
 
         //radial distance r, polar angle θ (theta), and azimuthal angle φ (phi)
         float r = targetDistance;
         float theta = Random.Range(-targetRelativeHorizontalFOV/2, targetRelativeHorizontalFOV/2); // Random horizontal angle change relative to facing
         float phi = 90 + Random.Range(-targetAbsoluteVerticalFOV/2, targetAbsoluteVerticalFOV/2) + (90 - currentPhi); // Random vertical angle change relative to horizon angle
 
         theta *= $$anonymous$$athf.Deg2Rad;
         phi *= $$anonymous$$athf.Deg2Rad;
 
         float x = r * $$anonymous$$athf.Sin (phi) * $$anonymous$$athf.Cos (theta);
         float y = r * $$anonymous$$athf.Sin (phi) * $$anonymous$$athf.Sin (theta);
         float z = r * $$anonymous$$athf.Cos (phi);
 
         Vector3 targetOffset = new Vector3(y, z, x); // Swap coordinates around to match unity coordinate system
 
         Vector3 targetPosition = Camera.main.transform.TransformPoint(targetOffset);
 
         return targetPosition;
     }
 
     // eulerAngles.x ranges from 90 (down) to 0 (forward), 360 (forward) to 270 (up)
     // This function transforms the range to -90 to 90
     private float centerEulerRotationRange(float ang) {
         if (ang >= 0 && ang <= 90) {
             return -ang;
         } else if (ang >= 270 && ang <= 360) {
             return 90 - (ang - 270);
         } else {
             throw new UnityException("I don't know how eulerAngles work");
         }
     }

avatar image elenzil ut_tommy · May 19, 2016 at 03:50 PM 0
Share

nice! glad you got it working. and looks like you definitely grappled w/ the details.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why does transform.rotate() do this? 1 Answer

Need help updating camera position to go above the player 2 Answers

(C#) Follow target (script) transform target PREFAB 0 Answers

Trouble converting transform.position to C# 1 Answer

transform.Rotate For an Certain Amount of Time 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