- Home /
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?
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.
I'm a new programmer, so I don't understand at all how to apply what you are telling me...
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;
}
}
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)
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.
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
Follow this Question
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