Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Application crashes with this script (orbit script)

Discussion in 'Scripting' started by morenostefanuto, Jan 22, 2021.

  1. morenostefanuto

    morenostefanuto

    Joined:
    Jan 1, 2020
    Posts:
    2
    Hi.This is my first post.
    I’m making a simulation of the solar system,using an approximation for orbit.It’s just the beginning of the project
    There is a sphere orbiting around the origin and i have added a script component to the sphere,that simulates the orbit.
    The application works for 10/15 minutes and then crashes and i don’t know why.
    The While inside the script never goes into an infinite loop and the problem persists even when using small values in the while loop.
    I’ve tried to use “deltatime”,but the result is the same o_O
    Any ideas to solve the problem?
    The script is below.

    /////////////////////////////////////////////
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class OrbitScript : MonoBehaviour
    {
    // Start is called before the first frame update
    float semiMajorAxis, eccentricity, orbitalPeriod;
    float x, y, z, r, m0, n, p, trueAnomaly0, trueAnomaly1, cosang, remainder, deltaT, tm;

    void Start()
    {
    deltaT = 5;
    tm = 0;
    r = 0;
    x = y = z = 0;
    orbitalPeriod = 1.77f;
    eccentricity = 0.4f;
    semiMajorAxis = 7;


    n = 2 * Mathf.PI / orbitalPeriod;

    m0 = n;
    trueAnomaly0 = m0;
    p = semiMajorAxis * (1 - eccentricity * eccentricity);
    }

    // Update is called once per frame
    void Update()
    {
    var deltaTime = Time.deltaTime;


    tm = tm + deltaT * deltaTime;
    m0 = n * tm;
    trueAnomaly1 = m0 + eccentricity * Mathf.Sin(trueAnomaly0);

    while (Mathf.Abs(trueAnomaly1 - trueAnomaly0) > 0.001f)
    {
    trueAnomaly0 = trueAnomaly1;
    trueAnomaly1 = m0 + eccentricity * Mathf.Sin(trueAnomaly0);

    }


    r = semiMajorAxis * (1 - eccentricity * Mathf.Cos(trueAnomaly1));

    cosang = (p / r - 1) / eccentricity;

    x = r * cosang;
    z = r * Mathf.Sqrt(Mathf.Abs(1 - cosang * cosang));



    remainder = trueAnomaly1 % (Mathf.PI * 2);
    if (remainder > Mathf.PI && remainder <Mathf.PI * 2)
    z = -z;

    y = transform.position.y;


    transform.position = new Vector3(x, y, z);

    }
    }
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    please report your problems with code tags,

    also your game is crashing becouse of your "While" loop in the "update" loop:

    Code (CSharp):
    1. void Update()
    2. {
    3. var deltaTime = Time.deltaTime;
    4.  
    5.  
    6. tm = tm + deltaT * deltaTime;
    7. m0 = n * tm;
    8. trueAnomaly1 = m0 + eccentricity * Mathf.Sin(trueAnomaly0);
    9.  
    10. while (Mathf.Abs(trueAnomaly1 - trueAnomaly0) > 0.001f)
    11. {
    12. trueAnomaly0 = trueAnomaly1;
    13. trueAnomaly1 = m0 + eccentricity * Mathf.Sin(trueAnomaly0);
    14.  
    15. }
     
    Joe-Censored likes this.
  3. morenostefanuto

    morenostefanuto

    Joined:
    Jan 1, 2020
    Posts:
    2
    While loop is necessary to have an approximation for the orbit.
    It is one of the most used techniques to approximate the orbit in a correct way and with the right accelerations during the orbit path,using orbital elements. I found it in internet.
    Without that loop, no correct simulation can be done.
     
  4. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    yes it might be used but not in an update loop :) , also check for infinit loop behaviour
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    That's very nice for you, and you are of course welcome to while loop all day long.

    However, just remember, until your while loop actually exits, only then will Unity
    Update()
    the frame.

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    You can place your loop into a coroutine and the same still applies, but at least then you can
    yield return
    , which is the ONLY thing you can put inside a loop if you want anything to appear onscreen.
     
    Terraya likes this.