Initial commit
This commit is contained in:
54
Assets/Scripts/SolarSystem.cs
Normal file
54
Assets/Scripts/SolarSystem.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class SolarSystem : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float G = 100f;
|
||||
private GameObject[] celestials;
|
||||
void Start()
|
||||
{
|
||||
celestials = GameObject.FindGameObjectsWithTag("Celestial");
|
||||
InitialVelocity();
|
||||
}
|
||||
private void FixedUpdate()
|
||||
{
|
||||
Gravity();
|
||||
}
|
||||
void Gravity()
|
||||
{
|
||||
foreach (GameObject a in celestials)
|
||||
{
|
||||
foreach (GameObject b in celestials)
|
||||
{
|
||||
if (!a.Equals(b))
|
||||
{
|
||||
float m1 = a.GetComponent<Rigidbody>().mass;
|
||||
float m2 = b.GetComponent<Rigidbody>().mass;
|
||||
float r = Vector3.Distance(a.transform.position, b.transform.position);
|
||||
a.GetComponent<Rigidbody>().AddForce(
|
||||
(b.transform.position - a.transform.position).normalized *
|
||||
(G * (m1 * m2) / (r * r))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void InitialVelocity()
|
||||
{
|
||||
foreach (GameObject a in celestials)
|
||||
{
|
||||
foreach (GameObject b in celestials)
|
||||
{
|
||||
if (!a.Equals(b))
|
||||
{
|
||||
float m2 = b.GetComponent<Rigidbody>().mass;
|
||||
float r = Vector3.Distance(a.transform.position, b.transform.position);
|
||||
a.transform.LookAt(b.transform);
|
||||
a.GetComponent<Rigidbody>().velocity +=
|
||||
a.transform.right * Mathf.Sqrt((G * m2) / r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user