2014-09-04 31 views
1

在C#中编写脚本时我很新颖,而且现在我一直在困扰这个问题。我制作了这个脚本,以便我的车可以在地图上移动,车轮在Z轴上旋转。脚本:在Unity 3D中使用C进行车轮转向#

using UnityEngine; 
using System.Collections; 

public class CarMovement : MonoBehaviour 
{ 
    public Transform wheelFLTrans; 
    public Transform wheelFRTrans; 
    public Transform wheelBRTrans; 
    public Transform wheelBLTrans; 
    public float MotorForce; 
    public float Steerforce; 
    public WheelCollider GumaPD; 
    public WheelCollider GumaPLj; 
    public WheelCollider GumaZD; 
    public WheelCollider GumaZLJ; 


    void Start() 
    { 
    } 
    // Update is called once per frame 


    void Update() 
    { 
     float v = Input.GetAxis("Vertical") * MotorForce; 
     float h = Input.GetAxis("Horizontal") * Steerforce; 
     GumaPD.motorTorque = v; 
     GumaPLj.motorTorque = v; 
     GumaPD.steerAngle = h; 
     GumaPLj.steerAngle = h; 
     wheelFLTrans.Rotate(Vector3.forward * GumaPLj.rpm * 2 * Mathf.PI/60.0f * Time.deltaTime * Mathf.Rad2Deg); 
     wheelFRTrans.Rotate(Vector3.forward * GumaPD.rpm * 2 * Mathf.PI/60.0f * Time.deltaTime * Mathf.Rad2Deg); 
     wheelBRTrans.Rotate(Vector3.forward * GumaZD.rpm * 2 * Mathf.PI/60.0f * Time.deltaTime * Mathf.Rad2Deg); 
     wheelBLTrans.Rotate(Vector3.forward * GumaZLJ.rpm * 2 * Mathf.PI/60.0f * Time.deltaTime * Mathf.Rad2Deg); 
     wheelFRTrans.eulerAngles = new Vector3(0f, Input.GetAxis("Horizontal"), 0f); 
    } 

} 

现在我的问题是:

我想补充轮转向,而我通过地图开我的车。就像当我按A或D键时,轮子将根据我按下的按键(A或D)转向。我已经试过这行代码:

 **wheelFRTrans.localEulerAngles = new Vector3(0, wheelFR.steerAngle, 0);** 

这工作,但后来由于某种原因,我的前轮停止转动:(有人可以帮助我这个请,我一直坚持已经与本作。 。天了:(我想,我的车轮可同时旋转和引导:/

我为我的英语不好对不起

感谢

回答

2

我解决了这个问题:!

Tires[0].transform.Rotate(Vector3.right,-BLWheel.rpm * 2 * Mathf.PI/60.0f * Time.deltaTime * Mathf.Rad2Deg,Space.Self); 
    Tires[1].transform.Rotate(Vector3.right,BRWheel.rpm * 2 * Mathf.PI/60.0f * Time.deltaTime * Mathf.Rad2Deg,Space.Self); 

    Tires[2].transform.Rotate(Vector3.right, FLWheel.rpm * 2 * Mathf.PI/60.0f * Time.deltaTime * Mathf.Rad2Deg,Space.Self); 
    Tires[3].transform.Rotate(Vector3.right, -FRWheel.rpm * 2 * Mathf.PI/60.0f * Time.deltaTime * Mathf.Rad2Deg,Space.Self); 

    Tires[2].transform.Rotate(Vector3.up, FLWheel.steerAngle - tempAngle,Space.World); 
    Tires[3].transform.Rotate(Vector3.up, FLWheel.steerAngle - tempAngle,Space.World); 
    tempAngle = FLWheel.steerAngle;