我正在關注如何在Unity中製作汽車的這段視頻(https://www.youtube.com/watch?v=kAEpLX3rfms)。但是我的車不會根據視頻的速度降低轉向角度。這個想法是,你可以在最慢的時候將汽車轉向最大50度,然後以最快的速度轉向10度。但它對汽車沒有影響。有人能告訴我如何解決這個問題嗎?如何根據Unity中的速度調整轉向角?
代碼:
public WheelCollider wheelFL;
public WheelCollider wheelFR;
public WheelCollider wheelRL;
public WheelCollider wheelRR;
public Transform wheelFL_Trans;
public Transform wheelFR_Trans;
public Transform wheelRL_Trans;
public Transform wheelRR_Trans;
public float lowSpeedSteerAngle;
public float highSpeedSteerAngle;
public float Torque;
public float centerOfMassHeight;
public float speed;
public GameObject speedometer;
public GameObject accelerator;
public GameObject brake;
public float pedalRotationFactor;
public float speedometerRotationFactor;
public float decelerationRate;
public float currentSpeed;
public float topSpeed;
public float maxReverseSpeed;
public GameObject trunk;
public Material[] brakeMaterials;
public float currentSteerAngle;
public float turnAmount;
private Vector3 com;
private Rigidbody rigidBody;
private float acceleratorSpeed;
private float brakeSpeed;
void Start() {
rigidBody = GetComponent<Rigidbody>();
com = rigidBody.centerOfMass;
com.y = centerOfMassHeight;
rigidBody.centerOfMass = com;
}
void FixedUpdate() {
currentSpeed = 2 * Mathf.PI * wheelFL.radius * wheelFL.rpm * 60/1000;
if(currentSpeed < topSpeed && currentSpeed > -maxReverseSpeed){
wheelFL.motorTorque = Torque * Input.GetAxis("Vertical");
wheelFR.motorTorque = Torque * Input.GetAxis("Vertical");
wheelRL.motorTorque = Torque * Input.GetAxis("Vertical");
wheelRR.motorTorque = Torque * Input.GetAxis("Vertical");
}else{
wheelFL.motorTorque = 0;
wheelFR.motorTorque = 0;
wheelRL.motorTorque = 0;
wheelRR.motorTorque = 0;
}
if(!Input.GetButton("Vertical")){
wheelFL.brakeTorque = decelerationRate;
wheelFR.brakeTorque = decelerationRate;
wheelRL.brakeTorque = decelerationRate;
wheelRR.brakeTorque = decelerationRate;
}else{
wheelFL.brakeTorque = 0;
wheelFR.brakeTorque = 0;
wheelRL.brakeTorque = 0;
wheelRR.brakeTorque = 0;
}
if(Input.GetAxis("Vertical") < 0){
//Changing The Materials
Material[] mats = trunk.GetComponent<Renderer>().materials;
mats[0] = brakeMaterials[1];
trunk.GetComponent<Renderer>().materials = mats;
}else{
//Reseting The Materials
Material[] mats = trunk.GetComponent<Renderer>().materials;
mats[0] = brakeMaterials[0];
trunk.GetComponent<Renderer>().materials = mats;
}
speed = GetComponent<Rigidbody>().velocity.magnitude * 15;
float speedFactor = 1 - (GetComponent<Rigidbody>().velocity.magnitude * 3.6f/topSpeed);
currentSteerAngle = highSpeedSteerAngle + ((lowSpeedSteerAngle - highSpeedSteerAngle) * speedFactor);
turnAmount = currentSteerAngle * Input.GetAxis("Horizontal");
wheelFL.steerAngle = turnAmount;
wheelFR.steerAngle = turnAmount;
}
void Update() {
wheelFL_Trans.Rotate(0, 0, wheelFL.rpm/60 * 360 * Time.deltaTime);
wheelFR_Trans.Rotate(0, 0, wheelFR.rpm/60 * 360 * Time.deltaTime);
wheelRL_Trans.Rotate(0, 0, wheelRL.rpm/60 * 360 * Time.deltaTime);
wheelRR_Trans.Rotate(0, 0, wheelRR.rpm/60 * 360 * Time.deltaTime);
Vector3 wheelSteerAngle = wheelFL_Trans.localEulerAngles;
wheelSteerAngle.y = turnAmount + 90;
wheelFL_Trans.localEulerAngles = wheelSteerAngle;
wheelFR_Trans.localEulerAngles = wheelSteerAngle;
if(Input.GetKeyDown(KeyCode.P)){
Vector3 resetPosition = new Vector3(transform.position.x, 1.5f, transform.position.z);
Quaternion resetRotation = Quaternion.Euler(0f, 0f, 0f);
//Reseting Velocity
GetComponent<Rigidbody>().velocity = new Vector3(0f, 0f, 0f);
//Reseting Rotation
transform.rotation = resetRotation;
//Reseting Position
transform.position = resetPosition;
//Reseting Motor Torque and Steering Angle
wheelFL.motorTorque = 0f;
wheelFR.motorTorque = 0f;
wheelRL.motorTorque = 0f;
wheelRR.motorTorque = 0f;
wheelFL.steerAngle = 0f;
wheelFR.steerAngle = 0f;
wheelRL.steerAngle = 0f;
wheelRR.steerAngle = 0f;
}
acceleratorSpeed = wheelFL.motorTorque;
brakeSpeed = wheelFL.motorTorque;
if(acceleratorSpeed < 0){
acceleratorSpeed = 0;
}
if(brakeSpeed > 0){
brakeSpeed = 0;
}
if(speed >= 350){
speed = 350;
}
speedometer.transform.localRotation = Quaternion.Euler(speed * speedometerRotationFactor - 100, 90f, 0f);
accelerator.transform.localRotation = Quaternion.Euler(acceleratorSpeed * pedalRotationFactor, 0f, 0f);
brake.transform.localRotation = Quaternion.Euler(brakeSpeed * pedalRotationFactor * -1, 0f, 0f);
}
沒有效果,汽車在低速時仍然以高速轉動相同的數量。 –
你的車的最高速度是多少?使currentSpeedAngle成爲公共變量或將其序列化,以便它在檢查器中顯示,然後檢查其值。如果速度降低到highSpeedSteerAngle的速度接近maxSpeed,則物理或控制器出現問題。 PS你沒有使用輸入來調節轉向? – EvilTak
最高速度是40.我剛剛製作了當前Speedpengle公開,價值徘徊在30左右。我也對turnAmount做了同樣的處理(來自gamedev的答案)。那只是回到原點而已;值不受速度的影響。我已經將代碼更新爲現在的順序。另外,通過使用輸入來調節轉向,你的意思是什麼? –