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



我正在使用牛顿引力定律和向心力计算在Unity中创建一个空间模拟,以重新创建行星的运动。最近,我尝试实现现实的质量,如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",这完全解决了我的问题

相关内容

最新更新