Unity C#不同寻常的讣告

Unity C#不同寻常的讣告,c#,2d,unity5,C#,2d,Unity5,我在做一个关于太空的游戏,像KSP和其他太空模拟器游戏。 正常轨道是一个圆。 我尝试使用碰撞R2D和点效应器2D来创建重力,但效果不太好 这是我的代码: using UnityEngine; using System.Collections; using System.Collections.Generic; public class Gravity : MonoBehaviour { public float Mass; private List<GameObject>

我在做一个关于太空的游戏,像KSP和其他太空模拟器游戏。

正常轨道是一个圆。

我尝试使用碰撞R2D和点效应器2D来创建重力,但效果不太好

这是我的代码:

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

public class Gravity : MonoBehaviour {

public float Mass;

private List<GameObject> InRangeObjects;

// Use this for initialization
void Start () {
    InRangeObjects = new List<GameObject>();
}

// Update is called once per frame
void Update () {
    ApplyGravity();
}

void OnTriggerStay2D(Collider2D col)
{
    List<GameObject> InRangeObjectstemp = new List<GameObject>();
    InRangeObjectstemp.Add(col.gameObject);
    InRangeObjects = InRangeObjectstemp;
    ////////////////////////////////////        
}

public void ApplyGravity()
{        
    foreach (GameObject Ga in InRangeObjects)
    {
        float distance = (Ga.transform.position - transform.position).magnitude;

        if (Ga.GetComponent<TotalDataStorer>().UseInGameGravity == true)
        {
            float GravitationalPule = (6.673e-11f * ((Mass * Ga.GetComponent<Rigidbody2D>().mass) / (distance * distance))) * Time.deltaTime;
            Debug.Log(GravitationalPule);
            Vector3 temp = Ga.transform.position - transform.position;
            Ga.GetComponent<Rigidbody2D>().AddForce(new Vector2(temp.x, temp.y) * GravitationalPule * -1) ;
        }
    }
}
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类引力:单一行为{
公众集会;
范围对象中的私有列表;
//用于初始化
无效开始(){
InRangeObjects=新列表();
}
//每帧调用一次更新
无效更新(){
应用重力();
}
无效OnTiggerstay2D(碰撞的R2D列)
{
List InRangeObjectstemp=新列表();
InRangeObjectstemp.Add(col.gameObject);
InRangeObjects=InRangeObjectstemp;
////////////////////////////////////        
}
公共无效应用权重()
{        
foreach(游戏对象Ga在范围对象中)
{
浮动距离=(Ga.transform.position-transform.position).magnity;
如果(Ga.GetComponent().Usingamegravity==true)
{
浮子引力定律=(6.673e-11f*((质量*Ga.GetComponent().Mass)/(距离*距离)))*时间增量;
调试日志(重力模块);
Vector3温度=Ga.transform.position-transform.position;
Ga.GetComponent().AddForce(新矢量2(温度x、温度y)*重力模块*-1);
}
}
}
}

我有一个像花一样的轨道,我怎么能解决这个问题呢?

问题是你的物体移动速度不够快,这是一个缓慢移动的外部物体的轨道,不是一个规则的“圆形”轨道,你必须调整你的物体移动速度。一般来说,一切都是正确的,这是一个轨道,在数学上是正确的,只是你需要得到正确的初始条件


哦,你在引力pull中拼错了pull,它是引力pull。

你可能还对地球同步轨道感兴趣。对不起,我不理解那个链接中的物理,我只是一名中一学生,你能给我一部分代码吗?谢谢你的帮助!如果你想做一个(有点)真实的模拟游戏,你需要理解物理和数学。如果不是这样,你只需要想出一些适合你的游戏的东西(换句话说:看起来“足够好”)