2014-09-04 63 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;