[英]Time.deltaTime not working as desired
我編寫了以下代碼:
using UnityEngine;
using System.Collections;
public class ObstacleSpawn : MonoBehaviour {
public PlayerScript pScript;
public ObstacleScript oScript;
public GameObject player;
public GameObject obstacle;
public float randomSpawnMin;
public float randomSpawnMax;
// Use this for initialization
void Start () {
InvokeRepeating ("Spawn", 2F, Random.Range (randomSpawnMin, randomSpawnMax));
}
// Update is called once per frame
void Update () {
}
void Spawn() {
if (pScript.isRight == true && pScript.inAir == false) {
obstacle.transform.localScale = new Vector3 (-1, 1, 1);
Instantiate (obstacle, player.transform.position + new Vector3 (0.05F, (4F + pScript.playerDimensionY + oScript.obstacleDimensionY) * Time.deltaTime, 0), Quaternion.identity);
} else if (pScript.isRight == false && pScript.inAir == false) {
obstacle.transform.localScale = new Vector3 (1, 1, 1);
Instantiate (obstacle, player.transform.position + new Vector3 (-0.05F, (4F + pScript.playerDimensionY + oScript.obstacleDimensionY) * Time.deltaTime, 0), Quaternion.identity);
}
}
}
在我的無效Spawn()中,我產生了一個稱為障礙的物品,應該在幾秒鍾內將其實例化為距離玩家所需的距離。 (等等。在擊中玩家之前,它應該相距2秒,或者1秒,依此類推。您可以在我的虛空中看到的是我實例化玩家位置上的障礙物+我測量的數量,然后乘以時間。 deltaTime。但是我的距離沒有改變,無論我是否改變乘以Time.deltaTime的值,我的對象之間的距離總是0.02秒。
如何解決此問題以自定義時間間隔?
編輯-為了澄清起見,這是2D,並且我還將顯示此示例以產生一些理解。 我嘗試也刪除了Time.deltaTime的乘法運算,因為程序移動無論如何都在deltaTime中運行:
void Spawn() {
if (pScript.isRight == true && pScript.inAir == false) {
obstacle.transform.localScale = new Vector3 (-1, 1, 1);
Instantiate (obstacle, player.transform.position + new Vector3 (0.05F, (4F + pScript.playerDimensionY + oScript.obstacleDimensionY), 0), Quaternion.identity);
} else if (pScript.isRight == false && pScript.inAir == false) {
obstacle.transform.localScale = new Vector3 (1, 1, 1);
Instantiate (obstacle, player.transform.position + new Vector3 (-0.05F, (4F + pScript.playerDimensionY + oScript.obstacleDimensionY), 0), Quaternion.identity);
}
}
顯示的代碼給了我0.58秒的間隔,這至少是一個間隔。 有了這種邏輯,我相信這段代碼應該提供一個1.16的間隔(間隔的兩倍):
void Spawn() {
if (pScript.isRight == true && pScript.inAir == false) {
obstacle.transform.localScale = new Vector3 (-1, 1, 1);
Instantiate (obstacle, player.transform.position + new Vector3 (0.05F, (8F + pScript.playerDimensionY + oScript.obstacleDimensionY), 0), Quaternion.identity);
} else if (pScript.isRight == false && pScript.inAir == false) {
obstacle.transform.localScale = new Vector3 (1, 1, 1);
Instantiate (obstacle, player.transform.position + new Vector3 (-0.05F, (8F + pScript.playerDimensionY + oScript.obstacleDimensionY), 0), Quaternion.identity);
}
}
但是,這給出的結果是0.92間隔,而不是1.16-這是我真正感到困惑的地方。
編輯上下文:
紅色障礙物始終會向下移動以擊中玩家,但我需要它在可調整的時間內(1秒距離,0.5秒距離等)生成。
僅在Update方法中才使用Time.deltaTime。 確保從Update()或LateUpdate()調用Spawn()方法。 更具體地說,deltaTime屬性存儲自上次更新調用以來的確切時間。 無論如何,我不確定您為什么要使該距離取決於幀率。 如果您知道播放器的速度,則應該使他的移動與時間有關,而與距離無關。 為了生成一個inverval對象,我建議使用協程。 像這樣:
private IEnumerator CoSpawn()
{
while (true)//you can put here any stop condition, otherwise this coroutine will run while gameObject exists and its not stopped by StopCoroutine()
{
//actual Spawn() call that will instantiate a new obstacle
yield return new WaitForSeconds(Random.Range(0, randomSpawnMax) + 2f); //pause for [2-(2+randomSpawnMax)] interval
}
}
您可以使用Start()方法中的StartCoroutine()代替InvokeRepeating()來啟動此協程。
更新:好的,如果您要計算障礙物的正確距離,那么我認為速度是恆定的。 如果是這樣,您可以通過以下方式向播放器移動障礙:
private void _Update()
{
var characterPosition = //cant say what it should be in your case
MoveTo(characterPosition);
}
private void MoveTo(Vector3 target)
{
//use 'transform' if you`re moving the obstacle from the script that is attached to it
//if your moving it from some scene controller or anywhere else, call [Obstacle].transform
transform.position = Vector3.MoveTowards(transform.position, target, Speed * Time.deltaTime);
}
在此代碼中,速度值應在兩種方法中均可用,一種是移動障礙物,另一種是實例化障礙物。 例如,您可以使Speed恆定或將其放入某些全局配置腳本中。 在Spawn()方法中,您可以計算正確的距離:
{
var timeToFall = 2f;//amount of time that the obstacle will fall
var position = new Vector3(0, timeToFall * Speed, 0);
}
Time.deltaTime
存儲幀之間的時間差。 只有在Update方法中使用Time.deltaTime
才有意義,該方法稱為每幀。
在您的情況下,您可以計算兩次Spawn()
調用之間經過的時間。 將當前的Time.time
存儲在一個屬性中,並計算最后一次Spawn
執行之間的差:
float lastUpdate = 0;
void Spawn() {
float myDelta = Time.time - lastTime;
lastUpdate = Time.time
然后使用myDelta
變量替換對Time.deltaTime
使用:
float lastUpdate = 0;
void Spawn() {
float myDelta = Time.time - lastTime;
lastUpdate = Time.time
if (pScript.isRight == true && pScript.inAir == false) {
obstacle.transform.localScale = new Vector3 (-1, 1, 1);
Instantiate (obstacle, player.transform.position + new Vector3 (0.05F, (4F + pScript.playerDimensionY + oScript.obstacleDimensionY) * myDelta, 0), Quaternion.identity);
} else if (pScript.isRight == false && pScript.inAir == false) {
obstacle.transform.localScale = new Vector3 (1, 1, 1);
Instantiate (obstacle, player.transform.position + new Vector3 (-0.05F, (4F + pScript.playerDimensionY + oScript.obstacleDimensionY) * myDelta, 0), Quaternion.identity);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.