Advertisement
shadowplaycoding

P005_SolarSystem

Apr 29th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SolarSystem : MonoBehaviour {
  5.  
  6.     public static SolarSystem SolarSystemInstance;
  7.  
  8.     void OnEnable()
  9.     {
  10.         SolarSystemInstance = this;
  11.     }
  12.  
  13.     // Use this for initialization
  14.     void Start () {
  15.    
  16.     }
  17.    
  18.     // Update is called once per frame
  19.     void Update () {
  20.  
  21.         Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  22.         RaycastHit hit = new RaycastHit();
  23.  
  24.         if (Physics.Raycast(mouseRay, out hit) && Input.GetMouseButtonDown(0))
  25.         {
  26.             Star star = Galaxy.GalaxyInstance.ReturnStarFromGameObject(hit.transform.gameObject);
  27.             Debug.Log("This Star is Called: " + star.starName + "\n" + "It Has " + star.numberOfPlanets + " Planets");
  28.  
  29.             Galaxy.GalaxyInstance.DestroyGalaxy();
  30.             CreateSolarSystem(star);
  31.         }
  32.  
  33.     }
  34.  
  35.     // This method creates the solar system view after a star is clicked on in the galaxy view
  36.     public void CreateSolarSystem(Star star)
  37.     {
  38.         GameObject starGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  39.         starGO.transform.position = Vector3.zero;
  40.         starGO.name = star.starName;
  41.         starGO.transform.SetParent(this.transform);
  42.  
  43.         for (int i = 0; i < star.planetList.Count; i++)
  44.         {
  45.             Planet planet = star.planetList[i];
  46.  
  47.             float distance = (i + 1) * 5;
  48.             float angle = Random.Range(0, 2 * Mathf.PI);
  49.  
  50.             Vector3 planetPos = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
  51.  
  52.             CreateSphereObject(planet, planetPos);
  53.         }
  54.  
  55.     }
  56.  
  57.     // This method creates a sphere object using the built in sphere model in unity
  58.     GameObject CreateSphereObject(Planet planet, Vector3 cartPosition)
  59.     {
  60.         GameObject planetGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  61.         planetGO.name = planet.planetName;
  62.         planetGO.transform.position = cartPosition;
  63.         planetGO.transform.SetParent(this.transform);
  64.  
  65.         return planetGO;
  66.     }
  67.  
  68.     /*
  69.    Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  70.    Removing this comment forfits any rights given to the user under licensing.
  71.    */
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement