所以我正在开发一款赛车游戏,但首先需要让我的车轮对撞机和车轮网格正常工作,但由于某种原因,它们似乎不同步。车轮碰撞器旋转以进行转向和加速,但车轮啮合不跟随,而是在测试时保持空闲。
这是我用来让车辆移动和车轮对撞机旋转的代码,据说这也是为了让车轮网格也旋转。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class AxleCougarInfo {
public WheelCollider LeftWheel;
public GameObject LeftWheelMesh;
public WheelCollider RightWheel;
public GameObject RightWheelMesh;
public bool motor;
public bool steering;
}
public class CougarController : MonoBehaviour {
public List<AxleCougarInfo> axleInfos;
public float maxMotorTorque;
public float maxSteeringAngle;
public void ApplyLocalPositionToVisual(AxleCougarInfo wheelPair)
{
wheelPair.LeftWheelMesh.transform.Rotate (Vector3.left, Time.deltaTime * wheelPair.LeftWheel.rpm * 10, Space.Self);
wheelPair.RightWheelMesh.transform.Rotate (Vector3.right, Time.deltaTime * wheelPair.RightWheel.rpm * 10, Space.Self);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void FixedUpdate() {
float motor = maxMotorTorque * Input.GetAxis ("Horizontal");
float steering = maxSteeringAngle * Input.GetAxis ("Vertical");
foreach (AxleCougarInfo axleInfo in axleInfos)
{
if (axleInfo.steering == true)
{
axleInfo.LeftWheel.steerAngle = steering;
axleInfo.RightWheel.steerAngle = steering;
}
if (axleInfo.motor == true)
{
axleInfo.LeftWheel.motorTorque = motor;
axleInfo.RightWheel.motorTorque = motor;
}
}
}
}