轮网不与轮子碰撞器一起旋转

时间:2017-08-04 17:19:23

标签: c# unity3d wheelcollider

所以我正在制作一个赛车游戏,但首先需要让我的车轮对撞机和车轮网格正常运行,但由于某种原因它们似乎并不是同步的。车轮碰撞器旋转以进行转向和加速,但车轮网格不会跟随,而是在测试时保持空转状态。

这是我用来使车辆移动和车轮碰撞器旋转的代码,据说这也意味着使车轮网格旋转。

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;
            }
        }
    }
}

0 个答案:

没有答案