松開 w、a、s、d 鍵(定向移動鍵)后,如何讓我的球滾動得更遠一點?目前,如果我停止按下 w、a、s、d,球會立即停止移動,但我希望它有動力并在我松開按鍵后繼續滾動一點。
這是我到目前為止所寫的內容:
public float speed = 6f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if(direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
uj5u.com熱心網友回復:
基本上添加物理和動量。按鍵均衡器專業知識力/轉移能量(E = 0.5mv^2 ro獲得速度)每次更新都會應用小數力(請參閱wiki,如果我沒記錯的話,常量 v^3)
結果是球逐漸加速,顆粒變慢并且不會立即改變方向。使摩擦力比使控制卡彭的力大。
uj5u.com熱心網友回復:
最好的方法是使用剛體與物理引擎互動(通過這種方式可以更容易地實作您正在尋找的效果)。但是,如果您更愿意使用字符控制器,那么我建議首先創建一個方法來執行您的輸入(示例/偽代碼):
void MovePlayer(moveDir) {
// Your moving logic
}
接下來,您可以創建一個計時器,在您的輸入停止后自動呼叫此方法的“x”時間(示例/偽代碼):
float rollTimer = 0;
void Update() {
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
// Add your logic to calculate when to activate the rolling effect
// possibly a toggle based on your horizontal & vertical input
timer = (horizontal != 0 && vertical != 0) ? 0 : 3;
// Add an if statement, so that you arent constantly setting timer to 0
if(timer > 0) {
while(timer > 0) {
MovePlayer(/* Simulated input */);
rollTimer -= time.deltaTime;
}
// Reset the timer
rollTimer = 0;
}
}
我現在無法對此進行測驗,因此您可能需要對其進行更多調整以適合您的確切代碼,但該概念仍然正確。這是我能想到的不使用剛體的最簡單的邏輯。如果您決定切換到剛體方法,那么您可以只使用 AddForce() 方法,該方法可以提供與物理引擎自然外觀相同的效果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/332872.html
上一篇:在回圈中使用協程
