如果沒有 Time.deltaTime,我的動作可以正常作業,但是當我將它添加到我的玩家動作中時,我可以從左到右正常移動,但我會慢慢跌倒,我不能跳躍
using Newtonsoft.Json.Bson;
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float horSpeed;
[SerializeField] private float vertSpeed;
private Rigidbody2D rb;
private Boolean isJumping;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
// we store the initial velocity, which is a struct.
var v = rb.velocity * Time.deltaTime;
if (Input.GetKey(KeyCode.Space) && !isJumping)
{
vf.y = vertSpeed;
isJumping = true;
}
if (Input.GetKey(KeyCode.A))
{
vf.x = -horSpeed;
}
if (Input.GetKey(KeyCode.D))
{
vf.x = horSpeed;
}
if (Input.GetKey(KeyCode.S))
{
vf.y = -vertSpeed;
}
rb.velocity = vf;
if(Input.GetKey(KeyCode.Escape))
SceneManager.LoadScene(0);
}
private void OnCollisionEnter2D(Collision2D collision)
{
isJumping = false;
}
}
我已經將它添加到我的敵人腳本中并且它作業正常,所以我不確定玩家移動與使用 transform.position 有什么不同
uj5u.com熱心網友回復:
你為什么要velocity乘以Time.deltaTime?
這用于將值從“每幀的某個單位”轉換為“每秒某個單位”的值。
速度已經是一個以每秒為單位的值。
剛體的線速度,以每秒為單位。
=> 在這里申請完全沒有意義Time.deltaTime。
對于您的 X 軸運動,它并沒有太大的區別,因為無論如何您每幀都連續應用該速度。在這里,您可能只想處理既不按下也不按下并設定左右的情況vf.x = 0。
但是對于 Y 軸,您只應用一次跳躍,只需在下一幀中將其除以大約 60(或任何您的幀速率)=> 您永遠不會給它跌倒或跳躍的機會。
一般來說,您應該修改FixedUpdate. 在您的特定情況下,這樣做很好Update,這只是多余的作業;)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/523510.html
標籤:C#unity3d二维
上一篇:黑屏時如何執行某些行?
