In order to overcome an exam of mine i have to create an orbital rotation without using any rigidbodies. So far i have managed to do it in one way or another but im failing to balance out the forces so my asteroids keep either crashing into the planet in the center or just fly away into the abyss. The question i would have is if somebody could think over my code quickly and help me getting the right numbers for that problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SplinterForce : MonoBehaviour //Asteroid
{
#region Variables
public Vector3 direction; //Is fed with Vector3 50/50/50 in the editor
public float power = 1;
#endregion
private void Start()
{
transform.LookAt(transform.parent);
direction = new Vector3(randomizer(), randomizer(), 0);
}
private void FixedUpdate ()
{
transform.Translate(direction * power * Time.deltaTime);
if (Vector3.Distance(transform.position, transform.parent.position) > 1000)
Destroy(gameObject);
}
public void AlterForce(Vector3 _direction)
{
direction += _direction;
}
private float randomizer()
{
float temp = Random.Range(40, 50);
if (Random.value > 0.5f)
temp *= -1;
return temp;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SunScript : MonoBehaviour //Center of everything
{
#region Variables
public float systemRotationSpeed;
public float gravitationalForce = 1;
#endregion
void Update()
{
Star.rotationRates = new Vector3(0, systemRotationSpeed * Time.deltaTime, 0);
}
private void FixedUpdate()
{
for (int i = 1; i < transform.childCount; i++)
{
Transform unit = transform.GetChild(i).transform;
float distance = Vector3.Distance(transform.position, unit.position);
float gforce = (gravitationalForce/ Mathf.Pow(distance, 2)) * 6674f;
Vector3 direction = Vector3.Normalize(transform.position - unit.position);
transform.GetChild(i).GetComponent<SplinterForce>().AlterForce(direction * gforce);
}
}
}