40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
public class solarSystem : MonoBehaviour
|
|
{
|
|
|
|
public GameObject sun;
|
|
public float speed;
|
|
public bool useDistance;
|
|
public List<GameObject> planets = new List<GameObject> ();
|
|
//a script to make a simple solar system, where every planet rotates according to the distance to the sun
|
|
void Start ()
|
|
{
|
|
Component[] components = GetComponentsInChildren (typeof(Transform));
|
|
//get all the planets inside the solar system
|
|
foreach (Component c in components) {
|
|
if (c.CompareTag ("moving")) {
|
|
planets.Add (c.gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Update ()
|
|
{
|
|
int planetsCount = planets.Count;
|
|
|
|
Vector3 sunPosition = sun.transform.position;
|
|
|
|
for (int i = 0; i < planets.Count; i++) {
|
|
float distance = GKC_Utils.distance (sunPosition, planets [i].transform.position);
|
|
|
|
if (useDistance) {
|
|
planets [i].transform.RotateAround (sunPosition, planets [i].transform.up, (speed / (distance / 20)) * Time.deltaTime);
|
|
} else {
|
|
planets [i].transform.RotateAround (sunPosition, planets [i].transform.up, speed * Time.deltaTime);
|
|
}
|
|
}
|
|
}
|
|
} |