《Unity案例:Armored Vehicle Kit》 |
版本 |
作者 |
参与者 |
完成日期 |
备注 |
UnityPro_ArmroedVehicleKit_V01_1.0 |
严立钻 |
|
2018.09.03 |
|
|
|
|
|
|
##《Unity案例:Armored Vehicle Kit》发布说明:
++++“Unity案例:Armored Vehicle Kit”是对Unity项目案例系列的一次尝试,这个例子来自Asset Store,这次探索,为后期大量的Asset Store案例剖析做个探索,由于代码剖析比较乏味,没有好的拓展经验,如何拓展,怎样才能起到好的示范作用,这些都需要摸索总结,以期能完成一套项目剖析的有效方案,给读者展示不一样的案例剖析;
++++“Unity案例:Armored Vehicle Kit”:装甲车辆系统,这是一个车辆模拟驾驶案例,有兴趣的童鞋可以试一试;
##《Unity案例:Armored Vehicle》目录:
#第一篇:项目介绍
#第二篇:代码结构
#第三篇:项目拓展
#第四篇:立钻哥哥带您汽车模拟实战
#第一篇:项目介绍 |
#第一篇:项目介绍
++++“Armored Vehicle Kit”:装甲车辆装备;
++++[Armored]:装甲的;
++++[Vehicle]:车辆;交通工具;
++++[Kit]:装备;
++[Sci-Fi Armored Vehicles Kit]
++++Animated Roof Mods, three pieces;
++++Eighteen premade vehicles;
++++Interior and Bottom;
++++Sixteen prefabs, to make your own vehicle;
++++Eight Body Variants;
++++Ten Roof Mods in total, action and support;
++++Mods are separated, ready to animate;
++++Seven paint jobs for body;
++++Three colors of dirt for tires;
++[Dot_Truck_Controller.cs(C#)]
++++Brakes;
++++Wheels reacting to surface;
++++Specify number of Axles;
++++Motor & Steering selection for each Axle;
++++Drag Wheel Collider & Visual Wheel;
++++Customize Motor Torque and Steering Angle;
++[Standard Materials]
++++Albedo;
++++Metalic;
++++Normal;
++++Occlusion;
++++Emission;
++[Cabin]
++++Interior is meant for FPS-kind of game-you look at vehicle, come to it and can see what is inside.
++++Steering Wheel is not separate;
++[Version 1.1]
++++Enhanced script: Added support of inverted steering to the wheels, for any axle;
#第二篇:代码结构 |
#第二篇:代码结构
++++B.1、Dot_Truck_Controller.cs
++++B.2、PostEffectsBase.cs
++++B.3、Tonemapping.cs
##B.1、Dot_Truck_Controller.cs |
++B.1、Dot_Truck_Controller.cs
++++ArmroedVehiclesKit\Assets\Armored Vehicles Kit\Scripts\Dot_Truck_Controller.cs
using UnityEngine; using System.Collections; using System.Collections.Generic;
[System.Serializable] public class Dot_Truck : System.Object{ public WheelCollider leftWheel; public GameObject leftWheelMesh; public WheelCollider rightWheel; public GameObject rightWheelMesh; public bool motor; public bool steering; public bool reverseTurn; } //立钻哥哥:public class Dot_Truck:System.Object{}
public class Dot_Truck_Controller : MonoBehaviour{ public List<Dot_Truck> truck_Infos; public float maxMotorTorque; public flaot maxSteeringAngle;
public void VisualizeWheel(Dot_Truck wheelPair){ Quaternion rot; Vector3 pos; wheelPair.leftWheel.GetWorldPose(out pos, out rot); wheelPair.leftWheelMesh.transform.position = pos; wheelPair.leftWheelMesh.transform.rotation = rot; wheelPair.rightWheel.GetWorldPose(out pos, out rot); wheelPair.rightWheelMesh.transform.position = pos; wheelPair.rightWheelMesh.transform.rotation = rot; }
public void Update(){ float motor = maxMotorTorque * Input.GetAxis(“Vertical”); float steering = maxSteeringAngle * Input.GetAxis(“Horizontal”); float brakeTorque = Mathf.Abs(Input.GetAxis(“Jump”));
if(brakeTorque > 0.001){ Debug.Log(brakeTorque); brakeTorque = maxMotorTorque; motor = 0; }else{ brakeTorque = 0; }
foreach(Dot_Truck truck_Info in truck_Infos){ if(truck_Info.steering == true){ truck_Info.leftWheel.steerAngle = truck_Info.rightWheel.steerAngle = ((truck_Info.reverseTurn) ? -1 : 1) * steering; }
if(truck_Info.motor == true){ truck_Info.leftWheel.motorTorque = motor; truck_Info.rightWheel.motorTorque = motor; }
truck_Info.leftWheel.brakeTorque = brakeTorque; truck_Info.rightWheel.brakeTorque = brakeTorque;
VisualizedWheel(truck_Info); } } //立钻哥哥:public void Update(){}
} //立钻哥哥:public class Dot_Truck_Controller:MonoBehaviour{} |
##B.2、PostEffectsBase.cs |
++B.2、PostEffectsBase.cs
++++\Assets\Armored Vehicles Kit\Standard Assets\Effects\ImageEffects\Scripts\PostEffectsBase.cs
using System; using UnityEngine;
namespace UnityStandardAssets.ImageEffects{ [ExecuteInEditMode] [RequireComponent(typeof(Camera))] public class PostEffectsBase : MonoBehaviour{ protected bool supportHDRTextures = true; protected bool supportDX11 = false; protected bool isSupported = true;
protected Material CheckShaderAndCreateMaterial(Shader s, Material m2Create){ }
protected Material CreateMaterial(Shader s, Material m2Create){ }
void OnEnable(){ isSupported = true; }
protected bool CheckSupport(){ return CheckSupport(false); }
public virtual bool CheckResources(){ Debug.LogWarning(“立钻哥哥:CheckResources() for ” + ToString() + “should be overwritten”); return isSupported; }
protected void Start(){ CheckResources(); }
protected bool CheckSupport(bool needDepth){ }
protected bool CheckSupport(bool needDepth, bool needHdr){ }
public bool DX11Support(){ return supportDX11; }
protected void ReportAutoDisable(){ }
bool CheckShader(Shader s){ }
protected void NotSupported(){ enabled = false; isSupported = false; return; }
protected void DrawBorder(RenderTexture dest, Material material){ }
} //立钻哥哥:public class PostEffectsBase:MonoBehaviour{}
} //立钻哥哥:namespace UnityStandardAssts.ImageEffects{}
|
++RenderTextureFormat namespace UnityEngine{ public enum RenderTextureFormat{ ARGB32, Depth, ARGBHalf, Shadowmap, RGB565, ARGB4444, ARGB1555, Default, ARGB2101010, DefaultHDR, ARGBFloat = 11, RGFloat, RGHalf, RFloat, RHalf, R8, ARGBInt, RGint, RInt, BGRA32, RGB111110Float = 22 } //立钻哥哥:public enum RenderTextureFormat{} } //立钻哥哥:namespace UnityEngine{} |
##B.3、Tonemapping.cs |
++B.3、Tonemapping.cs
++++\Assets\Armored Vehicles Kit\Standard Assets\Effects\ImageEffects\Scripts\Tonemapping.cs
using System; using UnityEngine;
namespace UnityStandardAssets.ImageEffects{ [ExecuteInEditMode] [RequireComponent(typeof(Camera))] [AddComponentMenu(“Image Effects/Color Adjustments/Tonemapping”)] public class Tonemapping : PostEffectBase{ public enum TonemapperType{ SimpleReinhard, UserCurve, Hable, Photographic, OptimizedHejiDawson, AdaptiveReinhard, AdaptiveReinhardAutoWhite, }; //立钻哥哥:public enum TonemapperType{}
public enum AdaptiveTexSize{ };
public TonemapperType type = TonemapperType.Photographic; public AdaptiveTexSize adaptiveTextureSize = AdaptiveTexSize.Square256;
public AnimationCurve rampCurve; private Texture2D curveTex = null;
public float exposureAdjustment = 1.5f;
public float middleGrey = 0.4f; public float white = 2.0f; public float adaptionSpeed = 1.5f;
public Shader tonemapper = null; public bool validRenderTextureFormat = true; private Material tonemapMaterial = null; private RenderTexture rt = null; private RenderTextureFormat rtFormat = RenderTextureFormat.ARGBHalf;
public override bool CheckResources(){ }
public float UpdateCurve(){ }
private void OnDisable(){ }
private bool CreateInternalRenderTexture(){ }
[ImageEffectTransformsToLDR] private void OnRenderImage(RenderTexture source, RenderTexture destination){ }
} //立钻哥哥:public class Tonemapping:PostEffectBase{}
} //立钻哥哥:namespace UnityStandardAssets.ImageEffects{} |
#第三篇:项目拓展 |
#第三篇:项目拓展
++++C.1、WheelCollider 轮碰撞器
[UnityAPI.WheelCollider轮碰撞器:https://blog.csdn.net/VRunSoftYanlz/article/details/82356217]
##C.1、WheelCollider 轮碰撞器 |
++C.1、WheelCollider 轮碰撞器
++++立钻哥哥:WheelCollider,用于车轮的特殊碰撞器;
namespace UnityEngine{ public sealed class WheelCollider : Collider{ //Properties public extern float brakeTorque{} public Vector3 center{} public extern float forceAppPointDistance{} public WheelFrictionCurve forwardFriction{} public extern bool isGrounded{} public extern float mass{} public extern float motorTorque{} public extern float radius{} public extern float rpm{} public WheelFrictionCurve didewaysFriction{} public extern float sprungMass{} public extern float steerAngle{} public extern float suspensionDistance{} public JointSpring suspensionSpring{} public extern float wheelDampingRate{}
//Constructors public WheelCollider();
//Methods public extern void ConfigureVehicleSubsteps(float speedThreshold, int stepsBelowThreshold, int stepsAboveThreshold); public extern bool GetGroundHit(out WheelHit hit); public extern void GetWorldPose(out Vector3 pos, out Quaternion quat); private extern void INTERNAL_get_center(out Vector3 value); private extern void INTERNAL_get_forwardFriction(out WheelFrictionCurve value); private extern void INTERNAL_get_sidewaysFriction(out WheelFrictionCurve value); private extern void INTERNAL_get_suspensionSpring(out JointSpring value); private extern void INTERNAL_set_center(ref Vector3 value); private extern void INTERNAL_set_forwardFriction(ref WheelFrictionCurve value); private extern void INTERNAL_set_sidewaysFriction(ref WheelFrictionCurve value); private extern void INTERNAL_set_suspensionSpring(ref JointSpring value); } //立钻哥哥:public sealed class WheelCollider:Collider{}
} //立钻哥哥:namespace UnityEngine{} |
++Description描述
++++立钻哥哥:class in UnityEngine/Inherits from: Collider;
++++用于车轮的特殊碰撞器;
++++轮子碰撞器用于车轮模型;(它模拟弹簧和阻尼悬挂装置,并使用一个基于滑动轮胎摩擦力模型计算车轮接触力)
++++车轮的碰撞检测是由从自身y轴中心向下投射射线;(车轮有一个半径和可以向下延长的suspensionDistance数)
++++车轮是由motorTorque,brakeTorque和steerAngle属性控制;
++++车轮碰撞器使用不同物理引擎的一个基于滑动的摩擦力模型来计算摩擦力;(这允许更加真实的行为)(而且使车轮忽略标准的PhysicMaterial设置)(通过改变车轮锁碰到的forwardFriction和sidewaysFriction来模拟不同的道路材质)
++Variables变量
++++brakeTorque(制动扭矩):制动的扭矩;(这个值必须为正)
++++center(中心):车轮的中心,基于物体的自身坐标空间;
++++forceAppPointDistance(着力点距离):从基本静止车轮测量的悬挂和轮胎的着力点;
++++forwardFriction(向前摩擦力):在车轮指向方向上的摩擦力的属性;
++++isGrounded(是否接地):表示车轮当前是否碰到什么(只读);
++++mass(质量):车轮的质量,以千克表示;(该值必须大于0,通常该值的范围是20~80)
++++motorTorque(马达扭矩):在轮轴上的马达扭矩;(根据方向正或负)
++++radius(半径):车轮的半径,基于物体的自身坐标空间;
++++rpm(转速):当前轮轴旋转速度,每分钟转速(只读);
++++sidewaysFriction(侧向摩擦力):轮胎侧面方向上的摩擦力的属性;
++++sprung(簧载质量):该车轮碰撞器支撑的质量;
++++steerAngle(转向角):转向角度,总是沿自身Y轴;
++++suspensionDistance(悬挂距离):车轮悬挂的最大伸展距离,基于自身坐标空间;
++++suspensionSpring(悬挂弹簧):车轮悬挂的参数;(通过应用线性力和阻尼力,悬挂试图达到目标位置)
++++wheelDampingRate(轮阻尼率):车轮的阻尼率;(必须大于0的值)
++Public Functions共有函数
++++ConfigureVehicleSubsteps():配置车辆子步参数;
++++GetGroundHit():(获取碰撞)获取车轮的地面碰撞数据;
++++GetWorldPose():(获取世界姿态)获取世界坐标空间车轮与地面接触、悬挂限制、转向角以及选择角度的姿态;
#第四篇:立钻哥哥带您汽车模拟实战 |
++立钻哥哥推荐的拓展学习链接(Link_Url):
++++立钻哥哥Unity 学习空间: http://blog.csdn.net/VRunSoftYanlz/
++++U3D小项目参考:https://blog.csdn.net/vrunsoftyanlz/article/details/80141811
++++Unity案例(Vehicle):https://blog.csdn.net/VRunSoftYanlz/article/details/82355876
++++JSON数据结构:https://blog.csdn.net/VRunSoftYanlz/article/details/82026644
++++HTC_VIVE开发基础:https://blog.csdn.net/VRunSoftYanlz/article/details/81989970
++++Unity5.x用户手册:https://blog.csdn.net/VRunSoftYanlz/article/details/81712741
++++Unity面试题ABC:https://blog.csdn.net/vrunsoftyanlz/article/details/78630687
++++Unity面试题D:https://blog.csdn.net/VRunSoftYanlz/article/details/78630838
++++Unity面试题E:https://blog.csdn.net/vrunsoftyanlz/article/details/78630913
++++Unity面试题F:https://blog.csdn.net/VRunSoftYanlz/article/details/78630945
++++Cocos2dx面试题:https://blog.csdn.net/VRunSoftYanlz/article/details/78630967
++++Lua快速入门篇(Xlua拓展):https://blog.csdn.net/VRunSoftYanlz/article/details/81173818
++++Lua快速入门篇(XLua教程):https://blog.csdn.net/VRunSoftYanlz/article/details/81141502
++++Lua快速入门篇(基础概述):https://blog.csdn.net/VRunSoftYanlz/article/details/81041359
++++框架知识点:https://blog.csdn.net/VRunSoftYanlz/article/details/80862879
++++游戏框架(UI框架夯实篇):https://blog.csdn.net/vrunsoftyanlz/article/details/80781140
++++游戏框架(初探篇):https://blog.csdn.net/VRunSoftYanlz/article/details/80630325
++++设计模式简单整理:https://blog.csdn.net/vrunsoftyanlz/article/details/79839641
++++专题:设计模式(精华篇):https://blog.csdn.net/VRunSoftYanlz/article/details/81322678
++++UML类图:https://blog.csdn.net/vrunsoftyanlz/article/details/80289461
++++Unity知识点0001:https://blog.csdn.net/vrunsoftyanlz/article/details/80302012
++++Unity知识点0008:https://blog.csdn.net/VRunSoftYanlz/article/details/81153606
++++U3D_Shader编程(第一篇:快速入门篇):https://blog.csdn.net/vrunsoftyanlz/article/details/80372071
++++U3D_Shader编程(第二篇:基础夯实篇):https://blog.csdn.net/vrunsoftyanlz/article/details/80372628
++++Unity引擎基础:https://blog.csdn.net/vrunsoftyanlz/article/details/78881685
++++Unity面向组件开发:https://blog.csdn.net/vrunsoftyanlz/article/details/78881752
++++Unity物理系统:https://blog.csdn.net/vrunsoftyanlz/article/details/78881879
++++Unity2D平台开发:https://blog.csdn.net/vrunsoftyanlz/article/details/78882034
++++UGUI基础:https://blog.csdn.net/vrunsoftyanlz/article/details/78884693
++++UGUI进阶:https://blog.csdn.net/vrunsoftyanlz/article/details/78884882
++++UGUI综合:https://blog.csdn.net/vrunsoftyanlz/article/details/78885013
++++Unity动画系统基础:https://blog.csdn.net/vrunsoftyanlz/article/details/78886068
++++Unity动画系统进阶:https://blog.csdn.net/vrunsoftyanlz/article/details/78886198
++++Navigation导航系统:https://blog.csdn.net/vrunsoftyanlz/article/details/78886281
++++Unity特效渲染:https://blog.csdn.net/vrunsoftyanlz/article/details/78886403
++++Unity数据存储:https://blog.csdn.net/vrunsoftyanlz/article/details/79251273
++++Unity中Sqlite数据库:https://blog.csdn.net/vrunsoftyanlz/article/details/79254162
++++WWW类和协程:https://blog.csdn.net/vrunsoftyanlz/article/details/79254559
++++Unity网络:https://blog.csdn.net/vrunsoftyanlz/article/details/79254902
++++C#事件:https://blog.csdn.net/vrunsoftyanlz/article/details/78631267
++++C#委托:https://blog.csdn.net/vrunsoftyanlz/article/details/78631183
++++C#集合:https://blog.csdn.net/vrunsoftyanlz/article/details/78631175
++++C#泛型:https://blog.csdn.net/vrunsoftyanlz/article/details/78631141
++++C#接口:https://blog.csdn.net/vrunsoftyanlz/article/details/78631122
++++C#静态类:https://blog.csdn.net/vrunsoftyanlz/article/details/78630979
++++C#中System.String类:https://blog.csdn.net/vrunsoftyanlz/article/details/78630945
++++C#数据类型:https://blog.csdn.net/vrunsoftyanlz/article/details/78630913
++++Unity3D默认的快捷键:https://blog.csdn.net/vrunsoftyanlz/article/details/78630838
++++游戏相关缩写:https://blog.csdn.net/vrunsoftyanlz/article/details/78630687
++++UnityAPI.Rigidbody刚体:https://blog.csdn.net/VRunSoftYanlz/article/details/81784053
++++UnityAPI.Material材质:https://blog.csdn.net/VRunSoftYanlz/article/details/81814303
++++UnityAPI.Android安卓:https://blog.csdn.net/VRunSoftYanlz/article/details/81843193
++++UnityAPI.AndroidJNI安卓JNI:https://blog.csdn.net/VRunSoftYanlz/article/details/81879345
++++UnityAPI.Transform变换:https://blog.csdn.net/VRunSoftYanlz/article/details/81916293
++++立钻哥哥Unity 学习空间: http://blog.csdn.net/VRunSoftYanlz/
--_--VRunSoft:lovezuanzuan--_--