在C#中编写脚本时我很新,而且我已经在这几天遇到了这个问题。我制作了这个脚本,以便我的汽车可以在地图中移动,并且车轮在Z轴上旋转。脚本:
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);**
这样可行,但由于某些原因,我的前轮停止旋转:(。有人可以帮我解决这个问题,我现在已经坚持了几天:(。我希望我的车轮可以旋转并在同一时间引导:/。
抱歉我的英语不好。
谢谢!
答案 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;