我最近一直在做一个项目,有一个山,森林和汽车的场景。
当汽车在地形上移动时,汽车会穿透地形
我只是想知道如何阻止这种情况发生。
在车上有Mesh Collider和RigidBody附件
在地形上有网格对撞机,凸面为假。
AzureVersion
答案 0 :(得分:0)
我之前已回答过这个问题,但无法找到另一个答案,将此标记为重复。您将WheelCollider
用于汽车。
您需要将WheelCollider
附加到汽车的所有车轮上。通过这样做,您的汽车将始终位于Terrain Collider
之上。
WheelCollider.motorTorque
用于向前或向后移动汽车。
WheelCollider.brakeTorque
用于制动汽车。
WheelCollider.steerAngle
用于驾驶汽车。
有很多教程,here是Unity的直接教程的一个例子:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class AxleInfo {
public WheelCollider leftWheel;
public WheelCollider rightWheel;
public bool motor;
public bool steering;
}
public class SimpleCarController : MonoBehaviour {
public List<AxleInfo> axleInfos;
public float maxMotorTorque;
public float maxSteeringAngle;
// finds the corresponding visual wheel
// correctly applies the transform
public void ApplyLocalPositionToVisuals(WheelCollider collider)
{
if (collider.transform.childCount == 0) {
return;
}
Transform visualWheel = collider.transform.GetChild(0);
Vector3 position;
Quaternion rotation;
collider.GetWorldPose(out position, out rotation);
visualWheel.transform.position = position;
visualWheel.transform.rotation = rotation;
}
public void FixedUpdate()
{
float motor = maxMotorTorque * Input.GetAxis("Vertical");
float steering = maxSteeringAngle * Input.GetAxis("Horizontal");
foreach (AxleInfo axleInfo in axleInfos) {
if (axleInfo.steering) {
axleInfo.leftWheel.steerAngle = steering;
axleInfo.rightWheel.steerAngle = steering;
}
if (axleInfo.motor) {
axleInfo.leftWheel.motorTorque = motor;
axleInfo.rightWheel.motorTorque = motor;
}
ApplyLocalPositionToVisuals(axleInfo.leftWheel);
ApplyLocalPositionToVisuals(axleInfo.rightWheel);
}
}
}