• 大小: 45.78MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-08-04
  • 语言: 其他
  • 标签: unity3d  3d赛车  

资源简介

unity3d 开发赛车的一个例子,可供新手参考。

资源截图

代码片段和文件信息

using UnityEngine;
using System.Collections;

public class AICarControl : MonoBehaviour {

//操纵前轮,用于转向
public WheelCollider FrontLeftWheel;
public WheelCollider FrontRightWheel;

//齿轮数组
public float[] GearRatio;
//当前档位
public int     CurrentGear=0;

public float EngineTorgue=600.0f;
public float MaxEngineRPM=3000.0f;
public float MinEngineRPM=1000.0f;
private float EngineRPM=0.0f;

public Gameobject waypointContainer;
private ArrayList waypoints;
private int currentWaypoint=0;

private float inputSteer=0.0f;
private float inputTorque=0.0f;
// Use this for initialization

void Start () {
//设置车的重心,使车更稳定
Vector3 centerOfMass=rigidbody.centerOfMass;
centerOfMass.y=-1.5f;
rigidbody.centerOfMass=centerOfMass;
GetWaypoints();
}

// Update is called once per frame
void Update () {
 //限制车的最大速度,调整阻力可能不是最好的做法。但它很简单,而且不会干扰物理系统的运行。
rigidbody.drag=rigidbody.velocity.magnitude/250;
NavigateTowardsWaypoint();
 //通过两个轮子的平均rpm,计算引擎rpm,然后切换档位
EngineRPM=(FrontLeftWheel.rpm+FrontRightWheel.rpm)/2*GearRatio[CurrentGear];
ShiftGears();


//设置换档的声音
audio.pitch=Mathf.Abs(EngineRPM/MaxEngineRPM)+1.0f;
if(audio.pitch>2.0)
{
audio.pitch=2.0f;
}

//最后设置轮子转动力矩。引擎力矩除以当前档位,乘以用户输入值。
//轮子力矩提供一个汽车前进的力。轮子的转动又会提高档位。
FrontLeftWheel.motorTorque=EngineTorgue/GearRatio[CurrentGear]*inputTorque;
FrontRightWheel.motorTorque=EngineTorgue/GearRatio[CurrentGear]*inputTorque;

//转动角度是任意数乘以用户输入值
FrontLeftWheel.steerAngle=10*inputSteer;
FrontRightWheel.steerAngle=10*inputSteer;
}
void ShiftGears()
{
int AppropriateGear=CurrentGear;
if(EngineRPM>=MaxEngineRPM)
{
AppropriateGear=CurrentGear;
for(int i=0;i {
if(FrontLeftWheel.rpm*GearRatio[i] {
AppropriateGear=i;
break;
}
}
CurrentGear=AppropriateGear;
}

if(EngineRPM<=MaxEngineRPM)
{
AppropriateGear=CurrentGear;
for(int j=GearRatio.Length-1;j>=0;j--)
{
if(FrontLeftWheel.rpm*GearRatio[j]>MinEngineRPM)
{
AppropriateGear=j;
break;
}
}
CurrentGear=AppropriateGear;
}
}
void GetWaypoints()
{
Transform[] potentialWaypoints =waypointContainer.GetComponentsInChildren();
waypoints= new ArrayList();
foreach(Transform potentialWaypoint in potentialWaypoints)
{
if(potentialWaypoint!=waypointContainer.transform)
{
waypoints.Add(potentialWaypoint);
}
}

}
void NavigateTowardsWaypoint()
{
Transform waypos=(Transform)waypoints[currentWaypoint];
Vector3 RelativeWaypointPosition=transform.InverseTransformPoint(waypos.position.x
transform.position.y
waypos.position.z);
inputSteer=RelativeWaypointPosition.x/RelativeWaypointPosition.magnitude;
if(Mathf.Abs(inputSteer)<0.5)
{
inputTorque=RelativeWaypointPosition.z/RelativeWaypointPosition.magnitude-Mathf.Abs(inputSteer);
}
else
{
inputTorque=0.0f;
}
if(RelativeWaypointPosition.magnitude<20)
{
cu

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3170  2013-02-15 17:46  myRaceCar\Assembly-CSharp-vs.csproj

     文件       3260  2013-02-15 17:46  myRaceCar\Assembly-CSharp.csproj

     文件       9564  2013-02-15 17:34  myRaceCar\Assembly-CSharp.pidb

     文件       3087  2013-02-15 17:46  myRaceCar\Assembly-Unityscript-firstpass-vs.unityproj

     文件       9307  2013-02-15 17:34  myRaceCar\Assembly-Unityscript-firstpass.pidb

     文件       3087  2013-02-15 17:46  myRaceCar\Assembly-Unityscript-firstpass.unityproj

     文件       3042  2013-02-15 17:46  myRaceCar\Assembly-Unityscript-vs.unityproj

     文件       6597  2013-02-15 17:34  myRaceCar\Assembly-Unityscript.pidb

     文件       3132  2013-02-15 17:46  myRaceCar\Assembly-Unityscript.unityproj

     文件      12612  2009-08-09 14:26  myRaceCar\Assets\.mayaSwatches\Road_Texture.psd.Composite.swatch

    I.A....     22712  2013-02-15 20:18  myRaceCar\Assets\AIRaceCar.prefab

     文件     197120  2009-08-05 20:04  myRaceCar\Assets\ArtWork\Car\Car_Model.mb

    I.A....      4220  2013-02-15 16:06  myRaceCar\Assets\ArtWork\Car\Materials\CarSolidTextures.mat

     文件       3801  2009-08-09 18:03  myRaceCar\Assets\ArtWork\Car\Materials\Car_Material.mat

    I.A....      4216  2013-02-15 14:36  myRaceCar\Assets\ArtWork\Car\Materials\Car_Texture.mat

     文件        494  2009-06-05 12:46  myRaceCar\Assets\ArtWork\Car\Shaders\ConstantColorUnderlayer_Shader.shader

     文件    2027997  2009-06-02 20:53  myRaceCar\Assets\ArtWork\Car\Textures\Car_Texture.psd

     文件       3748  2009-08-09 14:17  myRaceCar\Assets\ArtWork\Track\Materials\Road_Material.mat

    I.A....      4216  2013-02-15 14:36  myRaceCar\Assets\ArtWork\Track\Materials\Road_Texture.mat

     文件   21793873  2009-08-02 02:24  myRaceCar\Assets\ArtWork\Track\Textures\Road_Texture.psd

     文件     126640  2009-08-09 19:50  myRaceCar\Assets\ArtWork\Track\Track.mb

    I.A....     65896  2013-02-15 20:18  myRaceCar\Assets\Scene.unity

     文件       6232  2013-02-15 17:33  myRaceCar\Assets\scripts\Car Control\AICarControl.cs

     文件       3926  2013-02-15 16:48  myRaceCar\Assets\scripts\Car Control\CarControl.cs

     文件       2602  2013-02-15 14:32  myRaceCar\Assets\scripts\Car Control\WheelAlignment.cs

     文件        360  2013-02-15 15:57  myRaceCar\Assets\scripts\General\DrawWaypointGizmos_script.js

     文件     245804  2013-02-15 16:46  myRaceCar\Assets\Sound\CarEngine_Sound.wav

    I.A....      1046  2012-11-06 11:28  myRaceCar\Assets\Standard Assets\scripts\Camera scripts\MouseOrbit.js

    I.A....      1893  2012-11-06 11:28  myRaceCar\Assets\Standard Assets\scripts\Camera scripts\SmoothFollow.js

    I.A....       591  2012-11-06 11:28  myRaceCar\Assets\Standard Assets\scripts\Camera scripts\SmoothLookAt.js

............此处省略519个文件信息

评论

共有 条评论