In my game I have 3 objects with the tag “Body” after running this script (see below) everything works perfectly as intended. Unfortunately, after using debug.log i can tell you that after 1.01 minutes the gameobjects with the tag “Body” are deleted. I have no idea if this is a glitch or bad code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrbitPathFinder : MonoBehaviour
{
public int numSteps = 1000;
public float timeStep = 0.1f;
public float gravitationalConstant;
public bool relativeToBody;
public GameObject centralBody;
private GameObject[] bodies;
private bool gameOn = false;
void Start()
{
bodies = GameObject.FindGameObjectsWithTag("Body");
}
void Update()
{
if(Input.GetKeyDown("space"))
{
gameOn = true;
}
if(gameOn)
{
HideOrbits();
}
else
{
DrawOrbits();
}
}
void DrawOrbits()
{
var virtualBodies = new VirtualBody[bodies.Length];
var drawPoints = new Vector3[bodies.Length][];
int referenceFrameIndex = 0;
Vector3 referenceBodyInitialPosition = Vector3.zero;
for (int i = 0;i < virtualBodies.Length;i++)
{
virtualBodies _= new VirtualBody (bodies*);*_
drawPoints = new Vector3[numSteps];
if (bodies == centralBody && relativeToBody) {
referenceFrameIndex = i;
referenceBodyInitialPosition = virtualBodies*.position;*
}
}
for (int step = 0; step < numSteps; step++) { //for each step simulate the virtual bodies
Vector3 referenceBodyPosition = (relativeToBody) ? virtualBodies[referenceFrameIndex].position : Vector3.zero;//the position of the reference body in every step
// Update velocities
for (int i = 0; i < virtualBodies.Length; i++) { //for each virtual body
virtualBodies_.velocity += CalculateAcceleration (i, virtualBodies) * timeStep; //increases virtual objects velocity by the acceleration * speed of calculation_
}
// Update positions
for (int i = 0; i < virtualBodies.Length; i++) { //for each virtual object
Vector3 newPos = virtualBodies.position + virtualBodies_.velocity * timeStep; //new position is equal to the position and velocity times the time step_
virtualBodies*.position = newPos; //the virtual bodies position is then updated to the new position*
if (relativeToBody) { //if there is a central body
var referenceFrameOffset = referenceBodyPosition - referenceBodyInitialPosition;// the offset of the reference frame is the new position - initialposition
newPos -= referenceFrameOffset; //the offset is then subtracted from the new position
}
if (relativeToBody && i == referenceFrameIndex) { //if relative to body and the reference frame is the current index
newPos = referenceBodyInitialPosition; //then the position will be the initalposition
}
drawPoints*[step] = newPos; //The point where the line will be drawn then has another value added*
}
}
for (int bodyIndex = 0; bodyIndex < virtualBodies.Length; bodyIndex++)
{
var pathColour = bodies[bodyIndex].gameObject.GetComponent ().sharedMaterial.color; //sets the path to the colour of the object
for (int i = 0; i < drawPoints[bodyIndex].Length - 1; i++) { //for each bodies array of draw points length - 1
Debug.DrawLine (drawPoints[bodyIndex], drawPoints[bodyIndex][i + 1], pathColour);//draws a line on each draw point
Debug.Log(“yes”);
var lineRenderer = bodies[bodyIndex].gameObject.GetComponentInChildren ();
if (lineRenderer) {
lineRenderer.enabled = false; //disables the linerenderer in the child object of the planet
}
}
}
}
void HideOrbits()
{
for (int bodyIndex = 0; bodyIndex < bodies.Length; bodyIndex++) { //for each body
var lineRenderer = bodies[bodyIndex].gameObject.GetComponentInChildren ();
lineRenderer.positionCount = 0; //sets the amount of positions to 0
}
}
Vector3 CalculateAcceleration (int i, VirtualBody[] virtualBodies) {
Vector3 acceleration = Vector3.zero;//acceleration = 0
for (int j = 0; j < virtualBodies.Length; j++) {//foreach virtual body
if (i == j) { //if the index of the virtual body is the same as the real objects then continue
*continue;// /*
} // |
Vector3 forceDir = (virtualBodies[j].position - virtualBodies*.position).normalized;*
float sqrDst = (virtualBodies[j].position - virtualBodies*.position).sqrMagnitude;*
acceleration += forceDir * gravitationalConstant * virtualBodies[j].mass / sqrDst;
} //find the acceleration of the vitual object at that point using gravity formula
return acceleration;//return the value of the acceleration
}
class VirtualBody
{
public Vector3 position;
public Vector3 velocity;
public float mass;
public VirtualBody(GameObject g)
{
position = g.transform.position;
velocity = g.GetComponent().startVelocity;
mass = g.GetComponent().mass;
}
}
}