受 FPS 影响的行星

问题描述 投票:0回答:1

我正在尝试模拟我的star和我的planet两个物体之间的gravity,但它似乎受到FPS的影响,因为我的FPS上升和下降行星走得越来越快我可以放在哪里Time.deltaTime 来解决这个问题?

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

public class Attractor : MonoBehaviour {

    const float G = 667.4f;
    public Rigidbody star;
    public Rigidbody planet;
    
    void Update(){
        Vector3 direction = star.position - planet.position;
        float distance = direction.magnitude;

        if (distance == 0f)
            return;

        float forceMagnitude = G * (star.mass * planet.mass) / Mathf.Pow(distance, 2);
        Vector3 force = direction.normalized * forceMagnitude;

        planet.AddForce(force);
    }
}
unity3d simulation game-physics game-development
1个回答
0
投票

如前所述,物理计算应该在

FixedUpdate()

帧速率独立的 MonoBehaviour.FixedUpdate 物理计算消息。

物理学有自己的帧率,但不是可变的

Time.deltaTime
,而是固定的
Time.fixedDeltaTime
。尽管当您在
AddForce()
中使用
FixedUpdate()
时,您不应该乘以 delta,因为它是内部处理的。

ForceMode.Force:将输入解释为力(以牛顿为单位),并通过力 * DT / 质量的值改变速度。

您可以了解有关固定更新加力的更多信息。

顺便说一句,如果您应该将任何值乘以

Time.deltaTime
,那将是每次更新的任何变化值,因为发生的情况是,通过乘以
.1
,您将使用该值的一小部分,具体取决于最后一帧的长度。

快速的帧有一个小的增量,一个较慢的帧有一个较大的增量,导致较快的帧的值的一小部分和(...)

希望有帮助

© www.soinside.com 2019 - 2024. All rights reserved.