Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I add object on sphere?

Discussion in 'Scripting' started by boboyon, Oct 24, 2014.

  1. boboyon

    boboyon

    Joined:
    Oct 20, 2014
    Posts:
    3
    I try find some solution about rotate and calculate position from euler angle.

    Now, I have a planet, And I want to add gameobject on the planet's surface when I clicked mouse. I doing something like that...

    The object is on the planet,but they all cluster a small range, and they rotate isn't correct....




    This is my code with maincamera... can someone help me?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class addObjectonPlanet : MonoBehaviour {
    5.      public GameObject parentObj;
    6.      public GameObject[] gameObj;
    7.      int tag;
    8.      Vector3 mEuler;
    9.      Vector3 targetPos;
    10.      public float radius = 3;
    11.      int angleX, angleY, angleZ;
    12.      // Use this for initialization
    13.      void Start (){
    14.  
    15.      }
    16.      // Update is called once per frame
    17.      void Update () {
    18.          if (Input.GetButtonDown ("Fire1")) {
    19.              angleX = Random.Range (0, 360);
    20.              angleY = Random.Range (0, 360);
    21.              angleZ = Random.Range (0, 360);
    22.              mEuler = new Vector3 (angleX, angleY, angleZ);
    23.              targetPos = parentObj.transform.position + mEuler.normalized * radius;
    24.              tag = Random.Range (0, 3);
    25.              GameObject childObj = Instantiate (gameObj [tag], targetPos, Quaternion.Euler (mEuler)) as GameObject;
    26.              childObj.transform.parent = parentObj.transform;
    27.          }
    28.      }
    29. }
    30.  
    I just want to add object on the planet, Do not need to walk on the planet.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Random.onUnitSphere * radius
     
  3. boboyon

    boboyon

    Joined:
    Oct 20, 2014
    Posts:
    3
    yes, that can get the position on the surface, but how can I rotate the object at correct direction? just bottom on surface.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Quaternion.Lookrotation along the same random vector