• 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 albor09 · Sep 03, 2021 at 09:36 AM · 3dmathalgorithm

calculate position and rotation relatively to shpere (Planet)

Hi, Im trying come up with how spawn objects in random position (trees, stones) with right rotation relativly planet (sphere). Can anyone help me ? alt text

image-1.jpg (29.0 kB)
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 elenzil · Sep 08, 2021 at 03:59 PM 0
Share

@albor2004 - psst, if Captain Pineapple's answer got you unblocked on this, it's polite to 'Accept' the answer.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Captain_Pineapple · Sep 03, 2021 at 12:42 PM

Hey there and welcome to the forum.


rotation can be solved like this assuming that the z-Axis is the axis along your trees:

 tree.rotation = Quaternion.LookRotation(tree.position - sphere.position);

If the z-Axis is not the upward axis in your tree then the calculation is a bit more complex.

 var tmpVec = tree.position - sphere.position;
 tree.rotation = Quaternion.LookRotation(Vector3.Cross(Random.onUnitSphere ,tmpVec), tmpVec)

Important!: Due to the nature of a Vector cross product the latter calculation will fail if the random vector is exactly +- tmpVec. Keep this in mind and add a check for this if you use this solution.


As for the position. Depends. Do you want to add trees randomly? do you want a grid? there is no "correct" solution. Add more info on how you want to place trees.

In general you just have to normalize the vector and mutliply it by the spheres radius to get a vector that points exactly to the surface of your planet.

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 albor09 · Sep 03, 2021 at 12:57 PM 0
Share

Thx for answer, I will try it. Yes, I want spawn trees randomly on sphere or in random area.

avatar image
0

Answer by elenzil · Sep 03, 2021 at 06:34 PM

@captain_pineapple 's answer is excellent.

re positioning items randomly on a sphere, something like this is simple and robust, and has an expected runtime of about 2N. to get down to N is a little more involved.

 // array of transforms to put in position.
 // assuming these are children of the planet's transform,
 // and that the planet's transform is rooted at the center of the planet.
 Transform[] trees;
 
 foreach (Transform t in trees) {
     t.LocalPosition = Random.onUnitySphere * planetRadius;
     t.LocalRotation = Quaternion.LookRotation(t.LocalPosition);
 }
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

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

Related Questions

Retrieve 2d primitives from 3d colliders 1 Answer

3D. How to get 1 pixel to equal 1 degree camera FOV 0 Answers

Calculate collision between 2 rotated boxes without using BoxCollider (MATH) 2 Answers

How to scale/calculate GameObject Z distance to the real physical world... 0 Answers

Create a straight gradient equation with a grid of 3d objects 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