【问题标题】:Gravity works weird重力工作很奇怪
【发布时间】:2015-06-04 17:44:41
【问题描述】:

我想在 Unity 中进行简单的重力模拟,所以我编写了这个 UnityScript(使用来自 wikipedia.com 的重力信息):

var Type : String;
var Mass : double;
var Density : double;
var Volume : double;
var Surface : double;
var Radius : double;

var isStatic : boolean;
var onRails : boolean;

var totalForce : double;
var gravCon : double;
var SOI : double;
var inSOIof : GameObject;
var Distance : double;
function Start () {
gravCon = 0.000000000066738480808080808080;

Time.timeScale = 3;
Radius = gameObject.transform.localScale.x;
Volume = (4/3) * (Mathf.Pow(Radius,3) * Mathf.PI);
Surface = 4 * Mathf.PI * Mathf.Pow(Radius,2);
Density = Mass / Volume;

if(isStatic == true){ onRails = false; gameObject.GetComponent.<Rigidbody2D>().isKinematic = true;} 
 }

 function Update () {

  if(isStatic == false){
     if(onRails == false){
        Distance = Mathf.Sqrt(Mathf.Pow(gameObject.transform.position.x - inSOIof.transform.position.x, 2)+Mathf.Pow(gameObject.transform.position.y - inSOIof.transform.position.y, 2));
    totalForce = gravCon*((gameObject.GetComponent.<Specification>().Mass * inSOIof.GetComponent.<Specification>().Mass) / Mathf.Pow(Distance,2));
    gameObject.GetComponent.<Rigidbody2D>().AddForce((inSOIof.transform.position - gameObject.transform.position) * totalForce/Mass * Time.deltaTime);
    }
}

}

我创建了两个游戏对象:SolSpacecraft(它们是相同的,但 Sol 是静态的)。运行我的项目后,Spacecraft 的路径是这样的:

但它应该看起来像这样:

我做错了什么?

【问题讨论】:

    标签: unity3d unityscript gravity


    【解决方案1】:

    我解决了我的问题。 我换了

    gameObject.GetComponent.<Rigidbody2D>().AddForce((inSOIof.transform.position - gameObject.transform.position) * totalForce/Mass * Time.deltaTime);
    

    var direction : Vector3 = inSOIof.transform.position - transform.position;
        gameObject.GetComponent.<Rigidbody2D>().AddForceAtPosition((totalForce/Mass) * Time.deltaTime * direction.normalized, transform.position);
    

    【讨论】:

    • 如果您愿意,可以将此标记为正确答案。
    猜你喜欢
    • 2014-08-15
    • 2011-05-07
    • 2020-08-06
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    相关资源
    最近更新 更多