游戏对象在脚本中没有 Destroy() 的情况下自行删除

Game Object deletes itself without having Destroy() in the script

我正在 Unity 中创建一个 Space 模拟,使用牛顿万有引力定律和向心力计算来重新创建行星的运动。最近我尝试实现逼真的质量,例如 3.285e+23,并且我使用 massScale 1e-24f 将它们转换为可管理的数字。但是自从实施这些新质量并转换它们后,三颗行星中较小的那颗在运行后不久就开始自我删除,没有抛出任何错误。我还要补充一点,我的代码中也没有任何类型的 Destroy Line。

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

    public class PlanetVelocity : MonoBehaviour
    {
        private Gravity _gravity;
        public float v;
        public bool debugBool;
        public GameObject Sun;
        private Rigidbody rb;
        private Vector3 dif;

        public static List<GameObject> AllPlanets;
        private const float distScale = 1e-7f;
        private const float massScale = 1e-24f;
        private const float gravity = 6.674e-11f;
    
        public float mass = 5.972e24f;
    
        private Vector3 motion;
    
        [Header("Orbital Data")]
        public float velocity = 29.8f;
        public float distance = 148900000f;
    
    //TODO: Convert weight units to KG and use realistic weights for planetary bodies
    /*Calculate velocity using v = sqrt[(G M )/r]*/
    /*Get the Planet to look at the sun*/
    /*Apply V to the transform.right of the planet to have perpendicular motion*/
    
    void Start()
    {
        _gravity = FindObjectOfType<Gravity>();
        rb = GetComponent<Rigidbody>();
        rb.mass = mass * massScale;
    }

    void DebugClass()
    {
        if(debugBool)
        {
            Debug.Log($"Mass Scale {rb.mass}");
            //Debug.DrawRay(gameObject.transform.position, motion, Color.red);
           // Debug.Log($"Calculated Velocity = {v} Difference {dif} Motion {motion}");
        }
    }
    void ObjectLook()
    {
        transform.LookAt(Sun.transform);
    }

    
    // Update is called once per frame
    void Update()
    {
        DebugClass();
        ObjectLook();
        Vector3 sunPos = new Vector3(Sun.transform.position.x, Sun.transform.position.y, Sun.transform.position.z);
        Vector3 planetPos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y,
            gameObject.transform.position.z);

        Vector3 dif = sunPos - planetPos;
        float r = dif.sqrMagnitude;

        v = Mathf.Sqrt(_gravity.f * rb.mass) / r;
        //v = Mathf.Sqrt(_gravity.G * rb.mass / r ) ;

        Vector3 motion = transform.up * v;
        if (gameObject.transform.position.x > 0 && gameObject.transform.position.y > 0)
        {
            motion.x = Math.Abs(motion.x);
            motion.y = -Math.Abs(motion.y);
        }

        if (gameObject.transform.position.x > 0 && gameObject.transform.position.y < 0)
        {
            motion.x = -Math.Abs(motion.x);
            motion.y = -Math.Abs(motion.y);
        }

        rb.velocity = motion;
        
    }
}

这里是我的脚本,它根据太阳球体对撞机范围内的游戏对象来控制太阳的引力。

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

    public class Gravity : MonoBehaviour
    {
    
        public float f; //force

        private const float massScale = 1e-24f;
     
    
        public float mass = 1.989e+30f;
    
        public float G = 6.674e-11f; //Gravitational Constant
    
        private Rigidbody m1;//Mass 1 The Sun ( this Game Object)
        private Rigidbody m2;//Mass 2 Planet
        private Vector3 r1;
    
        public Vector3 FORCE;
    
        public bool debugBool;

    void Start()
    {
        Time.timeScale = 50f;
    }

    private void Awake()
    {
        m1 = GetComponent<Rigidbody>();
        m1.mass = mass * massScale;
    }

    private void DebugClass()
    {
        if (debugBool)
        {
            Debug.Log(m2.velocity); 
            Debug.Log("TRIGGERED");
            Debug.Log($"r1 {r1.normalized} FORCE {FORCE}");
        }
    }

    private void Update()
    {
        DebugClass();
    }

    private void OnTriggerStay(Collider other)
    {
        //declares rigidbody locally
        Rigidbody m2;
        //assigns other objects rigidbody to m2
        m2 = other.gameObject.GetComponent<Rigidbody>();
        
        // get sun position
        Vector3 Sun = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
        // planet
        Vector3 otherPlanet = new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y, other.gameObject.transform.position.z);
      
       
        //Difference between each object
        Vector3 r1 = Sun - otherPlanet;
     
        //Difference between each object squared
        float r2 = r1.sqrMagnitude;
        float distance = r1.magnitude;
        
        ///<summary>
        /// Calculates Force by Mutliplying Gravitational
        /// constant by the mass of each object divided 
        /// by the distance between each object squared
        /// <summary>
        f = G * m1.mass * m2.mass / r2;
        
        
        //Assigns the value r1 normalized between 0 and 1
        //multiplied by F independent of frames per second
        Vector3 FORCE = r1.normalized * f * Time.deltaTime;
        
        // Adds force to the other game objects rigidbody using the FORCE vector
        // Using the Force force mode
        m2.AddForce(FORCE);

    }
}
    



我的刚体变换上的浮点数非常小,因此 unity 在游戏对象溢出之前停用了它,我的解决方案是将我使用的物理计算乘以 'PhysicsMultiplier',这解决了我的问题问题完全