因此,球使用 AddForce 自行向前移動。代碼有效,但唯一的問題是它的速度變化。這取決于顯示幕的尺寸。當啟用 MaximizeOnPlay 時,球移動得太快,而當它關閉時,球移動緩慢。這是我的 PlayerContoller 腳本(如果需要 RigidBody,請告訴我,我會附上它的螢屏截圖):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
private bool isGrounded = false;
private Rigidbody _ourRigidbody;
[SerializeField] private float verticalSpeed = 0.7f;
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit(Collision other)
{
if (other.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
void Start()
{
_ourRigidbody = GetComponent<Rigidbody>();
}
void LateUpdate()
{
if (isGrounded)
{
verticalSpeed = 0.7f;
_ourRigidbody.mass = 1;
}
if (!isGrounded)
{
verticalSpeed = -5;
_ourRigidbody.mass = 90;
}
float verticalDelta = verticalSpeed * Time.fixedDeltaTime;
Vector3 force = new Vector3(0, verticalDelta, verticalSpeed);
_ourRigidbody.AddForce(force);
}
}
uj5u.com熱心網友回復:
如果你不關心現實物理,只想有一個恒定的速度,你可以使用_ourRigidbody.velocity(<your velocity vector>)。
相反,如果您想確保物理行為是確定性的,則必須將其FixedUpdate用作控制器回圈。這可確保在固定的時間間隔內施加所需的力。例子:
void FixedUpdate() //This changed
{
if (isGrounded)
{
verticalSpeed = 0.7f;
_ourRigidbody.mass = 1;
}
if (!isGrounded)
{
verticalSpeed = -5;
_ourRigidbody.mass = 90;
}
float verticalDelta = verticalSpeed * Time.fixedDeltaTime;
Vector3 force = new Vector3(0, verticalDelta, verticalSpeed);
_ourRigidbody.AddForce(force);
}
uj5u.com熱心網友回復:
LateUpdate 被稱為每一幀。
在其中,您不想使用Time.fixedDeltaTimewhich 是FixedUpdate被呼叫的間隔,而是Time.deltaTime
從最后一幀到當前幀的間隔(以秒為單位)
float verticalDelta = verticalSpeed * Time.deltaTime;
一般來說,你應該在FixedUpdate. 甚至他們的 Unity 建議仍然使用,Time.deltaTime因為
當從內部呼叫 this 時
MonoBehaviour.FixedUpdate,它回傳Time.fixedDeltaTime。
所以使用Time.deltaTime你總是在保存方面。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/349299.html
上一篇:獲取帶有特定標簽的每個按鈕
