[英]Projectile Physics - Why do i get a different result when using Time.sinceLevelLoad over Time.delatime in Unity?
I'm playing around with projectile physics on objects in Unity. 我在Unity的物体上玩射弹物理学。 In this case I'm just using a sphere object and calculating it's vector and the path it should take based on gravity (leaving other forces like friction out for the moment).
在这种情况下,我只是使用一个球体对象,并根据重力来计算它的矢量和它应该走的路径(暂时不包括摩擦力)。
The formula I'm using is the following: s = ut + ½at²
and the code solution is simple enough as follows: 我正在使用的公式如下:
s = ut + ½at²
,代码解很简单,如下所示:
using UnityEngine;
using System.Collections;
public class TestProjectileForce : MonoBehaviour {
// Create vector components to calculate force & position
private float force = 1.8f;
private float g = 9.8f;
private float angle = 30f;
private float fx;
private float fy;
// projectile calculate
private float dis_x;
private float dis_y;
// Use this for initialization
void Start () {
// calculate x & y vector
fx = Mathf.Round (force * Mathf.Cos(Mathf.Deg2Rad * angle));
fy = Mathf.Round (force * Mathf.Sin(Mathf.Deg2Rad * angle));
}
// Update is called once per frame
void Update () {
// s = ut + 1/2at2
dis_x = (float)(fx * Time.timeSinceLevelLoad);
dis_y = (float)(fy + Time.smoothDeltaTime + (0.5) * (-g) * Time.timeSinceLevelLoad * Time.timeSinceLevelLoad);
Debug.Log (dis_x+", "+dis_y);
transform.Translate (new Vector2(dis_x, dis_y));
}
}
I noticed however, that the expected result i should get varies greatly depending on if i use Time.sinceLevelLoad
over Time.deltaTime
. 但是我注意到,我应该得到的预期结果在很大程度上取决于我
Time.sinceLevelLoad
在Time.deltaTime
上使用Time.deltaTime
。 Can anyone explain to me why this is? 谁能向我解释为什么? When using
Time.sinceLevelLoad
i get the expected result of the ball arcing and dropping given the force of gravity acting upon it, however with Time.deltatime
the balls will just shoot off and not arc as expected. 当使用
Time.sinceLevelLoad
时,如果重力作用在球上,我会得到预期的Time.sinceLevelLoad
和落球效果,但是使用Time.deltatime
,球会Time.deltatime
射出,而不是预期的弧度。
Time.deltaTime is the time interval being processed by this Update. Time.deltaTime是此更新正在处理的时间间隔。 At 30fps it is 1/30s assuming no variance in frame rate.
假设帧速率没有变化,则在30fps下为1/30秒。 For physics you should use FixedUpdate which is always a fixed interval.
对于物理学,您应该使用FixedUpdate,它始终是固定的时间间隔。
Time.timeSinceLevel load is at it says, the elapsed game time since loading the level. Time.timeSinceLevel加载表明它是从加载关卡以来经过的游戏时间。
They are very different concepts. 它们是非常不同的概念。 The first can be used to derive an iterative solution, the second to evaluate a function over time (assuming you always want to evaluate from start of level load).
第一个可用于派生迭代解决方案,第二个可用于随时间推移评估函数(假设您始终希望从级别加载开始就进行评估)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.