Speherical Planet spawning objects
I'm trying to make a nature simulator with object spawning. However, I can't seem to figure out how to actually get the objects to spawn right. I want them to spawn with their upward point facing AWAY from the centre. Basically, Planet up is object up. But I don't know how to do this. I have tried loads but... can't seem to get it to work.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NatureMaker : MonoBehaviour { public GameObject[] NatureObject = new GameObject[5]; public int SelectedNatureObject;
public GameObject planet;
public Vector3 planetPos;
public void Start()
{
planetPos = planet.transform.position;
SelectedNatureObject = 0;
}
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(this.transform.position, this.transform.forward, out hit, 10f))
{
Instantiate(NatureObject[SelectedNatureObject], hit.point, *The rotation bit*);
}
else
{
}
}
}
}
Answer by Kantic · May 07, 2020 at 01:55 PM
To orient an object downward(relative to the planet) you can use tree.transform.rotation = Quaternion.lookRotation(positionOfThePlanet - positionOfTheObject). However this will induce a rotation on the local Y axis of the object, which, might not be that big of a problem for trees if you dont want this to happen use Quaternion.fromtoRotation:
this video explain how to walk on planet and should help you with yourproblem