[英]Not clamping the Y Velocity in AddForce?
我正在使用 addforce 讓角色向右移動,為了防止玩家無限上升到 10 馬赫,我限制了速度。 效果很好,但是如果我鉗制速度,它也會鉗制 y 速度,從而阻止我跳躍。
if (moveDir.magnitude >= 0.1f)
{
controller.AddForce(moveDir * moveSpeed, ForceMode.Impulse);
//Needs a fix because it also clamps the Y velocity which is needed so it doesn't fall slowly
controller.velocity = Vector3.ClampMagnitude(controller.velocity, moveSpeed);
//Animation Call
animationCaller.Animation_Move(animator);
}
else
{
//Needs a fix because it also clamps the Y velocity which is needed so it doesn't fall slowly
controller.velocity = Vector3.ClampMagnitude(controller.velocity, 0f);
controller.angularVelocity = Vector3.ClampMagnitude(controller.angularVelocity, 0f);
//Animation Call
animationCaller.Animation_Idle(animator);
}
嘗試將它們全部分離到它們自己的向量 3 中,然后在它平靜下來后重新添加回 y 速度,但這會導致速度永遠不會下降的問題。
controller.AddForce(moveDir * moveSpeed, ForceMode.Impulse);
/*
Vector3 clampVelocity;
clampVelocity.x = 0;
clampVelocity.y = controller.velocity.y;
clampVelocity.z = 0;
*/
controller.velocity = Vector3.ClampMagnitude(controller.velocity, moveSpeed); // + clampVelocity;
當您鉗制矢量的大小時,您限制了它的標量大小。 這意味着,無論方向如何,它都不能超過給定的尺寸。
如果要對非垂直分量施加限制,可以將 Z 和 X 分量投影到 2D 向量上,然后將其夾緊,然后相應地在原始 Vector3 上設置 X 和 Z 分量。
為了很好地集成此行為,明智的做法是將分配邏輯添加到速度矢量的屬性中。 但是,這可能需要您擴展正在使用的物理 object class。 由於您調用了 GameObject 本身的速度成員(這只是它找到的第一個物理組件的速度的重定向),您可以更改您正在分配的物理 controller 的類型,或者正確隱藏您的繼承速度游戲對象。
我認為你很接近,但你只想先夾緊 XZ,然后保持當前的 Y 速度,例如
controller.AddForce(moveDir * moveSpeed, ForceMode.Impulse);
// get velocity
var currentVelocity = controller.velocity;
// store Y axis
var currentY = currentVelocity.y;
// reset Y axis - so it doesn't influence the clamp
currentVelocity.y = 0;
// clamp magnitude of XZ
currentVelocity = Vector3.ClampMagnitude(currentVelocity, moveSpeed);
// re-apply Y axis
currentVelocity.y = controller.velocity.y;
controller.velocity = currentVelocity;
盡管我的問題是為什么還要使用AddForce
?
您也可以直接像這樣直接組合速度
// already clamp on the input
var velocity = Vector3.ClampMagnitude(moveDir * moveSpeed);
// also take over current Y axis
velocity.y = controller.velocity.y;
controller.velocity = velocity;
那么究竟是什么
controller.velocity = Vector3.ClampMagnitude(controller.velocity, 0f);
應該做的? 如果您將幅度限制為0
,您也可以恰到好處Vector3.zero
。 如果你只想重置兩個組件 XZ 你可以簡單地做
var velocity = controller.velocity;
velocity.x = 0;
velocity.z = 0;
controller.velocity = velocity;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.