1、需要在Rigid Body下构造轮子,否则在Editor窗口看不到wheel Collider的轮廓
2、需要把车的mess设置大一点1000,否则很容易被轮子给弹出地面
3、轮子的模型和Wheel Collider在Rotation都为0时坐标轴方向一致,否则之后旋转不对
4、轮胎的高度位置需要一致,防止离地或其他原因导致车无法走动
5、只需要给两个轮子添加动力,其他的轮子会跟着旋转
6、轮胎真实的模型旋转和位置需要从whell collider中获取
7、没有设置动力的轮胎只需要设置旋转即可
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Car : MonoBehaviour {
private WheelCollider Wheel;
public Transform realWheal;
public float angle = 10;
public float motor = 10;
// Use this for initialization
void Awake () {
Wheel = gameObject.GetComponent<WheelCollider>();
}
// Update is called once per frame
void FixedUpdate() {
//方向盘旋转的轮胎角度
Wheel.steerAngle = angle;
//正为向前动力,负为向后动力,0为空挡滑行
Wheel.motorTorque = motor;
Vector3 pos;
Quaternion rot;
Wheel.GetWorldPose(out pos,out rot);
realWheal.position = pos;
realWheal.rotation = rot;
}
}
下载地址:Unity工程
参考:
https://docs.unity3d.com/ScriptReference/WheelCollider.html