當我松開角色控制按鈕時,角色本身會繼續移動大約半秒鐘。我希望角色在我松開控制按鈕后立即停止。我嘗試了不同的方法:AddForceand velocity,但都是徒勞的。
此外,我嘗試在角色的 Inspector 中調整質量和阻力動量,但沒有幫助。
public class CapsuleMovement : MonoBehaviour
{
Rigidbody rb;
Vector3 playerMovement;
[SerializeField] float speed = 50;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
ProccessCapsuleMovement();
}
void ProccessCapsuleMovement ()
{
playerMovement = new Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
playerMovement.Normalize();
rb.velocity = playerMovement * speed * Time.deltaTime;
}
}
uj5u.com熱心網友回復:
不要正常化!如果輸入幅度實際上小于
1您仍然將其歸一化為幅度1!而是使用
Vector3.ClampMagnitude它只限制上限的大小但允許它更小另一點可能
GetAxis是實際上“平滑”而不是立即應用!釋放按鈕后,它實際上會隨著時間的推移而減少。因此,由于您對向量進行了歸一化,因此1在釋放按鈕后它會保持一段時間的大小。您可能更愿意為此使用
GetAxisRaw它。然后分配的速度,當你不希望乘
Time.deltaTime!這僅在您要將值從固定值轉換value per frame為與幀速率無關的值時才需要value per second。速度已經是一個vector per second所以洗掉* Time.deltaTime.
所以像例如
playerMovement = Vector3.ClampMagnitude(new Vector3 (Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")), 1f);
rb.velocity = playerMovement * speed;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/365503.html
上一篇:如何強制二人組SpringCloudsleuth-SpringMVC/SpringRest處理在控制器的處理程式方法引數上設定的@SpanTag注釋
