资源简介
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-Unitysc
文件 9307 2013-02-15 17:34 myRaceCar\Assembly-Unitysc
文件 3087 2013-02-15 17:46 myRaceCar\Assembly-Unitysc
文件 3042 2013-02-15 17:46 myRaceCar\Assembly-Unitysc
文件 6597 2013-02-15 17:34 myRaceCar\Assembly-Unitysc
文件 3132 2013-02-15 17:46 myRaceCar\Assembly-Unitysc
文件 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\ConstantColorUnderla
文件 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\sc
文件 3926 2013-02-15 16:48 myRaceCar\Assets\sc
文件 2602 2013-02-15 14:32 myRaceCar\Assets\sc
文件 360 2013-02-15 15:57 myRaceCar\Assets\sc
文件 245804 2013-02-15 16:46 myRaceCar\Assets\Sound\CarEngine_Sound.wav
I.A.... 1046 2012-11-06 11:28 myRaceCar\Assets\Standard Assets\sc
I.A.... 1893 2012-11-06 11:28 myRaceCar\Assets\Standard Assets\sc
I.A.... 591 2012-11-06 11:28 myRaceCar\Assets\Standard Assets\sc
............此处省略519个文件信息
- 上一篇:网上商城毕业设计SSH
- 下一篇:CTP多账户多策略-交易程序
相关资源
- Unity3D PuppetMaster 布娃娃系统 骨骼动画
- UNITY3D 吃豆人 源码下载263056
- Unity3D 自定义树组件树形菜单
- 传智播客Unity3D视频教程— 6-传智播客
- Unity3D真实地形提取插件
- Monster.unitypackage
- unity3D汽车小游戏+源代码
- Unity3D Tetris 俄罗斯方块
- Unity3D贪吃蛇源码
- Unity3D 使用Vuforia Vuforia Engine 8.0发布
- unity 3d迷宫 NPC 小地图
- Final IK 2.0
- unity简单的2D飞机大战游戏
- NGUI v3.12.0 Unity3D2017~2018
- 基于OpenGL的3D赛车游戏的研究与实现源
- Unity3D VText 2.0.2
- 传智播客Unity3D视频教程—3-案例:地
- 《Unity3D ShaderLab开发实战详解》源代码
- Unity3D特效包 100种unity特效
- unity3d小游戏源码
- 次表面散射玉石
- 3D MAX 9(2009) FBX插件 官方版
- unity3d解包工具
- SALSA with RandomEyes for unity3d
- Unity3D坦克大战 源代码3D游戏开发
- unity简单的2D飞机大战
- Unity3D StarTrooper多人游戏源码(可运行
- Unity3D官方地形包,TerrainAsset.unitypac
- Unity3D 坦克射击游戏完整工程文件
- 超好用的公路创建插件RoadBuilder.zip
评论
共有 条评论