简体   繁体   English

射弹物理-在Unity中使用Time.sinceLevelLoad而不是Time.delatime时为什么会得到不同的结果?

[英]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.sinceLevelLoadTime.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.

相关问题 我如何获得时间在Unity中使用StreamWriter和DateTime保存到文件(我在使用c#)? - How do I get the time to save to a file using StreamWriter and DateTime in Unity (I'm using c#)? 团结会随着时间的推移而愈合 - Unity heal over time 随时间增加浮点值时查找剩余时间[Unity] - Finding the remaining time when increasing a float value over time [Unity] 即使使用 FixedUpdate() 或 Time.DeltaTime() 物理也不一致 - Physics inconsistent even when using FixedUpdate() or Time.DeltaTime() 为什么在Visual C#项目中使用Measurement Studio .NET控件时,为什么在运行时出现未经许可的程序集错误消息 - Why Do I Get an Unlicensed Assemblies Error Message at Run Time When Using Measurement Studio .NET Controls in a Visual C# Project 为什么我要在一个小时后使用大西洋标准时间为圣马丁安排时间? - Why do I get a time one hour later using Atlantic Standard Time for St. Martin? Unity - 当弹丸击中它时,为什么我的球会朝相反的方向滚动? - Unity - Why is my ball rolling in the opposite direction when a projectile hits it? 使用DPAPI加密时,为什么会得到不同的输出? - Why do I get different outputs when encrypting using DPAPI? 团结:如何找出要从哪个角度射击物理弹头才能降落在某个位置 - Unity: How can I find out what angle I need to shoot from for a physics projectile to land in a certain spot Unity:如何随时间更改 Shader Graph 的属性(浮动)? - Unity : How do I change a Shader Graph's property (float) over time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM