やりたいこと
Unityで車を操作できるようにしたくてサイトを参考にしながらWheelColliderを使って製作していたのですが、3Dのタイヤをつけ始めたあたりから、車は動くものの、タイヤが横に回るようになってしまいました。
よく見ると、コライダーの向きがおかしくなっていたので車の向きを変えて実装しなおしたのですが、それでも実行したとたんに同じ状態になってしまいます...
参考にしたサイト
コード
サイトにも書いてありますが一応載せておきます。
Car.cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Car : MonoBehaviour 6{ 7 public List<AxleInfo> axleInfos; 8 public float maxMotorTorque; 9 public float maxSteeringAngle; 10 11 public void ApplyLocalPositionToVisuals(WheelCollider collider) 12 { 13 Transform visualWheel = collider.transform.GetChild(0); 14 Vector3 position; 15 Quaternion rotation; 16 collider.GetWorldPose(out position, out rotation); 17 visualWheel.transform.position = position; 18 visualWheel.transform.rotation = rotation; 19 } 20 21 public void FixedUpdate() 22 { 23 float motor = maxMotorTorque * Input.GetAxis("Vertical"); 24 float steering = maxSteeringAngle * Input.GetAxis("Horizontal"); 25 foreach (AxleInfo axleInfo in axleInfos) 26 { 27 if (axleInfo.steering) 28 { 29 axleInfo.leftWheel.steerAngle = steering; 30 axleInfo.rightWheel.steerAngle = steering; 31 } 32 if (axleInfo.motor) 33 { 34 axleInfo.leftWheel.motorTorque = motor; 35 axleInfo.rightWheel.motorTorque = motor; 36 } 37 ApplyLocalPositionToVisuals(axleInfo.rightWheel); 38 ApplyLocalPositionToVisuals(axleInfo.leftWheel); 39 } 40 } 41} 42[System.Serializable] 43public class AxleInfo 44{ 45 public WheelCollider leftWheel; 46 public WheelCollider rightWheel; 47 public bool motor; //駆動輪か? 48 public bool steering; //ハンドル操作をしたときに角度が変わるか? 49}
調べたこと
自分が調べたところ、コライダーの向きを変えることはできないそうなのですが、車もタイヤも全く向きなんて変えていないのに、なぜコライダーの向きがおかしくなってしまうのか全く分かりません...
追記:
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。