【发布时间】:2021-11-08 21:11:25
【问题描述】:
我正在 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 不会简单地随机销毁对象 .. 必须在某个地方调用
Destroy!为了找出答案,您可以使用一个简单的技巧:在您的对象上放置一个类似public class WhatDestroyesMe : MonoBehaviour { private void OnDestroy () { Debug.LogError("I WAS JUST DESTROYED!!!!"); } }的类,然后您可以在控制台的堆栈跟踪中看到导致该对象被破坏的确切原因。此外,您还可以在调试行设置断点,并在 Debugging your code 时查看何时发生这种情况 -
或者您的对象甚至没有被破坏,只是被吸入太阳或被踢出可见度?你检查过层次结构吗?
-
您好 Derhugo 感谢您的回复!当我检查层次结构时,游戏对象实际上已从中删除,而没有出现加载游戏对象的破坏,这使我认为统一正在破坏对象而没有被告知!我还要补充一点,在我停止游戏后该对象会重新出现,我将使用您的调试方法查看它是否被破坏并在此处添加另一条评论!
-
好吧,显然它正在被销毁,但我的代码中没有任何内容告诉它被销毁,所以这就是我的困惑所在,我将用我的 ide 的快照编辑我的帖子在断点上识别
-
这里是 IDE 结果的 imgur imgur.com/a/31WMKVh
标签: unity3d game-physics physics