為什麼在 WebGl 構建中腳本化的精靈移動和旋轉變慢?

拉奎布爾伊斯蘭教

我是一名初學 Unity 開發人員。我創造了這個。

在此處輸入圖片說明

遊戲開始時,右上角的嚮導會從屏幕外進入。然後停下來扔斧頭。這最終擊中了偵探並導致他摔倒。

巫師和斧頭有他們的腳本。嚮導沒有附加任何組件。斧頭有一個剛體(重力比例設置為 0)和一個盒子碰撞器。

嚮導腳本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class WizardMover : MonoBehaviour
{
    public Vector3 move;
 
    // Start is called before the first frame update
    void Start()
    {
       
    }
 
    // Update is called once per frame
    void Update()
    {  
        // Wizard stops at at a specific postion
        if (transform.localPosition.x > 5.79)
        {
            transform.localPosition -= move;
        }
    }
}

和斧頭的腳本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class AxeFly : MonoBehaviour
{
    public Vector3 move;
    public Vector3 rotate;
 
    // Start is called before the first frame update
    void Start()
    {
       
    }
 
    // Update is called once per frame
    void Update()
    {
        transform.localPosition -= move;
 
        // Axe starts rotating when thrown
        if (transform.localPosition.x < 5.80)
        {
            transform.Rotate(rotate);
        }
    }
}

當我在 Unity 中玩這個時,一切都按預期工作。但是在 WebGL 構建中,嚮導和斧頭的腳本化移動和旋轉速度變得非常慢。為什麼會這樣?

雨果

您的代碼取決於幀速率

這意味著在可以以更高 FPS(每秒幀數)運行的更強大的設備上,您的Update方法在與 FPS 較低的弱設備上相同的絕對實時時間內被更頻繁地調用。

因此,當然,如果您在同一時間內更頻繁地移動固定距離,則物體總體上會走得更遠,反之亦然。


對於 Unity 中的連續動畫,您應該始終使用 Time.deltaTime

從最後一幀到當前幀的間隔(以秒為單位)(只讀)。

(有時甚至Time.unscaledDeltaTime在您想忽略當前的情況下Time.timeScale

基本上任何值從轉換value per framevalue per second

transform.localPosition -= move * Time.deltaTime;

transform.Rotate(rotate * Time.deltaTime);

moverotate相應地調整為每秒的預期絕對值

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章