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 albertadroer · Dec 03, 2021 at 10:03 AM · gravityplanets

Placing objects help needed

I'm making a game based on spheric planet generation, and it's weird and hard to work on hand placed assets to the equatorial and south pole zones of the planet, since the engine is made to be worked on flat worlds. Is there any way so when you place an asset, it automatically rotates towards the normal of the non-flat terrain?

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

Answer by MarekRimal · Dec 03, 2021 at 02:52 PM

The surface normal at the asset location is just (asset.position - sphere.position).normalized. You can then for example in the Start method of the Asset script do something like transform.up = normal. That makes all the assets "standing" on the surface when you click Play.

If you would like this to work even in edit mode, the best solution would be probably to make a new scrip with mentioned Start method and put [ExecuteInEditMode] before the class declaration, see more here https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html.

Comment
Add comment · Show 5 · 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 albertadroer · Dec 03, 2021 at 04:16 PM 0
Share

I'm a new programmer, so I don't understand at all how to apply what you are telling me...

avatar image MarekRimal albertadroer · Dec 03, 2021 at 04:29 PM 0
Share

Ah ok. Then try placing this script on assets you would like to place on the planet:

 [ExecuteInEditMode]
 class CorrectAssetPlacer : MonoBehaviour
 {
   [SerializeField] private Transform _planet; // Drag the planet game object to this field in inspector
 
   private void Start
   {
     // This should make the asset correctly rotated
     Vector3 newUpVector = (transform.position - _planet.transform.position).normalized;
     transform.up = newUpVector;
   }
 }


avatar image Eno-Khaon MarekRimal · Dec 03, 2021 at 08:55 PM 0
Share

Another way to approach this could be to perform automatic modifications in the editor when needed:

 #if UNITY_EDITOR
 void OnValidate() // Called when editor values are changed
 {
     RotateToGround();
 }
 #endif
 
 void RotateToGround()
 {
     if(_planet != null)
     {
         Vector3 newUpVector = (transform.position - _planet.transform.position).normalized;
         transform.up = newUpVector;
     }
 }

(This example uses "RotateToGround()" as a generic function that could be used when new GameObjects are placed during runtime)

avatar image MarekRimal albertadroer · Dec 03, 2021 at 04:31 PM 0
Share

Btw are your planets perfect spheres or are there hills on them? Since I guess it must be very difficult to place your objects on the planets by hand.

avatar image albertadroer MarekRimal · Dec 06, 2021 at 12:30 AM 0
Share

It's a halo-like planet :) For now, I'm making the terrain on Blender with multiple LODS to simulate some sort of render distance... Since idk how to code a proper halo terrain that's modifiable on unity.

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

139 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

Related Questions

Rigidbody gravity between planets 1 Answer

Unity3D PlanetGravity system? C# 1 Answer

Objects acting under each other's gravity 4 Answers

Faux Gravity, spinning at bottom of sphere 0 Answers

I Need help with gravity 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