我一直在研究一個簡單的太空飛行器控制器,它允許移動、偏航、滾動和俯仰。它在每個軸上單獨作業都很好,但是當我同時偏航和滾動、俯仰和移動等時,旋轉是不正確的。我相信某些事情會發生兩次,因為當車輛處于 90 度俯仰角時,它應該垂直移動時水平移動(90 度旋轉時偏離 90 度)。在 45 處,它位于中間的某個位置。在 0 時,它按預期作業。
我的完整代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Input : MonoBehaviour
{
private Rigidbody rb;
public float moveAcceleration = 50f;
public float maxMoveSpeed = 500f;
public float turnAcceleration = 30f;
public float maxTurnSpeed = 100f;
public float rotAcceleration = 30f;
public float maxRotSpeed = 100f;
private float moveSpeed = 0;
private float pitchSpeed = 0;
private float yawSpeed = 0;
private float rollSpeed = 0;
private Vector3 angle = new Vector3 (0, 0, 0);
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKey("w")) {
moveSpeed = moveAcceleration * Time.deltaTime;
} else if(Input.GetKey("s")) {
moveSpeed -= moveAcceleration * Time.deltaTime;
}
if(moveSpeed > 10f) {
moveSpeed -= 5f * Time.deltaTime;
}else if(moveSpeed < -10f) {
moveSpeed = 5f * Time.deltaTime;
} else if(!Input.GetKey("w") && !Input.GetKey("s")) {
moveSpeed = 0f;
}
moveSpeed = Mathf.Clamp(moveSpeed, -maxMoveSpeed, maxMoveSpeed);
if(Input.GetKey("a")) {
yawSpeed = turnAcceleration * Time.deltaTime;
} else if(Input.GetKey("d")) {
yawSpeed -= turnAcceleration * Time.deltaTime;
}
if(yawSpeed > 10f) {
yawSpeed -= 5f * Time.deltaTime;
}else if(yawSpeed < -10f) {
yawSpeed = 5f * Time.deltaTime;
} else if(!Input.GetKey("a") && !Input.GetKey("d")) {
yawSpeed = 0f;
}
yawSpeed = Mathf.Clamp(yawSpeed, -maxTurnSpeed, maxTurnSpeed);
if(Input.GetKey("left")) {
rollSpeed = rotAcceleration * Time.deltaTime;
} else if(Input.GetKey("right")) {
rollSpeed -= rotAcceleration * Time.deltaTime;
}
if(rollSpeed > 10f) {
rollSpeed -= 5f * Time.deltaTime;
}else if(rollSpeed < -10f) {
rollSpeed = 5f * Time.deltaTime;
} else if(!Input.GetKey("left") && !Input.GetKey("right")) {
rollSpeed = 0f;
}
rollSpeed = Mathf.Clamp(rollSpeed, -maxRotSpeed, maxRotSpeed);
if(Input.GetKey("up")) {
pitchSpeed = turnAcceleration * Time.deltaTime;
} else if(Input.GetKey("down")) {
pitchSpeed -= turnAcceleration * Time.deltaTime;
}
if(pitchSpeed > 10f) {
pitchSpeed -= 5f * Time.deltaTime;
}else if(pitchSpeed < -10f) {
pitchSpeed = 5f * Time.deltaTime;
} else if(!Input.GetKey("up") && !Input.GetKey("down")) {
pitchSpeed = 0f;
}
pitchSpeed = Mathf.Clamp(pitchSpeed, -maxTurnSpeed, maxTurnSpeed);
this.transform.Translate(moveSpeed * transform.forward * Time.deltaTime);
angle = Time.deltaTime * ((pitchSpeed * transform.right) (rollSpeed * transform.forward) (yawSpeed * transform.up));
//Debug.Log(transform.forward);
this.transform.eulerAngles = angle;
}
}
相關位在最后(加速度和預期的作業),就在角度設定的位置。變換是正確的(我已經檢查過),但應用的動作不是。你看到問題了嗎?
謝謝,我很感激!
編輯:GameObject 上也只有 1 個腳本實體,只是為了進一步縮小范圍。
編輯 2:我發現這個論壇帖子似乎有同樣的問題,但我認為我沒有做過與此人相同的事情。也許是一個好的起點?
編輯3:我覺得“角度 = ...”線是罪魁禍首,但我不知道為什么。也許看看那里。
uj5u.com熱心網友回復:
使用Quaterion該類避免云臺鎖定并將當前旋轉與新的附加旋轉相乘。我修改了腳本的最后幾行:
> angle = new Vector3(pitchSpeed, yawSpeed, rollSpeed);
> transform.rotation *= Quaternion.Euler(angle);
> transform.Translate(moveSpeed * transform.forward * Time.deltaTime, Space.World);
你會想要大大減少這些,我將它們除以 10,這看起來很順利:
public float turnAcceleration = 30f;
public float maxTurnSpeed = 100f;
public float rotAcceleration = 30f;
public float maxRotSpeed = 100f;
不確定您是否希望偏航處于按下或反轉的方向,目前如果您按下左它偏航右,如果需要,您可以反轉這些:
if(Input.GetKey("a")) {
yawSpeed = turnAcceleration * Time.deltaTime;
} else if(Input.GetKey("d")) {
yawSpeed -= turnAcceleration * Time.deltaTime;
}
moveSpeed , pitchSpeed, yawSpeed, rollSpeed如果您正在制作太空游戲,您可能需要考慮使用https://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html或https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp進行平滑處理。 html以使動作更逼真。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/484717.html
上一篇:播放影片5次
