当前位置: 首页 > news >正文

【Unity每日一记】WheelColider组件汽车游戏的关键

在这里插入图片描述


👨‍💻个人主页:@元宇宙-秩沅

👨‍💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

👨‍💻 本文由 秩沅 原创

👨‍💻 收录于专栏unity每日一记

🅰️推荐文章


⭐【软件设计师高频考点暴击】

⭐【Unityc#专题篇】之c#系统化大礼包】

⭐【unity数据持久化】数据管理类_PlayerPrfs

⭐【unity本站最全系列】unity常用API大全一篇文章足以


WheelColiderz组件汽车游戏的关键

在这里插入图片描述


文章目录

    • 🅰️推荐文章
    • WheelColiderz组件汽车游戏的关键
    • 🎶(==A==) 关键API知识
    • 🎶(==B==) 参数一览
    • 🎶(==C==) 扭矩力
    • 🎶(==D==) 阿克曼转向
    • 🎶(==E==) 汽车下压力
    • 🎶(==F==) 汽车质心
    • 🎶(==G==) 发动机相关
    • 🎶(==H==) 自动挡位变速箱
    • 🅰️系统路线学习点击跳转
  • 四最终代码
    • CarMoveContorl
    • CameraFllow
    • InputMana



🎶(A 关键API知识


API解释
motorTorque扭矩力:
brakeTorque制动扭矩:刹车
Radius碰撞器车轮半径
Wheel Damping rate车轮阻尼率
GetWorldPose(out wheelPosition, out wheelRotation);获取碰撞器当前的空间位置和空间角度。
steerAngle车轮碰撞器的转向。
GetGroundHit车轮的地面碰撞数据。
rpm当前轮轴转速(以每分钟转数为单位)。
isGrounded车轮是否在空中(只读)
WheelHit参数说明
colliderT另一个碰撞机的轮子正在撞击。
force施加在接触上的力的大小。
forwardDir滚轮指向的方向。.
forwardSlip轮胎在滚动方向上打滑。加速滑移为负,制动滑为正
normal接触点的正常。.
pointT车轮与地面之间的接触点。
sidewaysDir车轮的侧向方向。l.
sidewaysSlip侧身滑 轮胎向侧向打滑。.
rpm当前轮轴转速(以每分钟转数为单位)。(只读).

🎶(B 参数一览


在这里插入图片描述


🎶(C 扭矩力


知识百科:什么是扭矩力?
扭矩是指发动机运转时从曲轴端输出的平均力矩,俗称为发动机的“转劲”,是 发动机性能 的一个重要参数,扭矩越大,发动机输出的“劲”越大,曲轴转速的变化也越快,汽车的爬坡能力、起步速度和加速性也越好。


🎶(D 阿克曼转向


引用:阿克曼转向是一种现代汽车的转向方式,也是移动机器人的一种运动模式,在汽车转弯的时候,内外轮转过的角度不一样,内侧轮胎转弯半径小于外侧轮胎

  • 后轮距尺寸设置为1.5f ,轴距设置为2.55f ,radius 默认为6,radius 越大旋转的角度看起来越小
 if (horizontal > 0 ) {
//后轮距尺寸设置为1.5f ,轴距设置为2.55f ,radius 默认为6,radius 越大旋转的角度看起来越小wheels[0].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius + (1.5f / 2))) * horizontal;wheels[1].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius - (1.5f / 2))) * horizontal;} else if (horizontal < 0 ) {                                                          wheels[0].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius - (1.5f / 2))) * horizontal;wheels[1].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius + (1.5f / 2))) * horizontal;} else {wheels[0].steerAngle =0;wheels[1].steerAngle =0;}

🎶(E 汽车下压力


知识百科: 什么是下压力
下压力是车在行进中空气在车体上下流速不一产生的,使空气的总压力指向地面从而增加车的抓地力.

速度越大,下压力越大,抓地更强,越不易翻车
在这里插入图片描述

  • 关键代码
  //-------------下压力添加-----------------//速度越大,下压力越大,抓地更强rigidbody.AddForce(-transform.up * downForceValue * rigidbody.velocity .magnitude );

🎶(F 汽车质心


知识百科:什么是质心?——质量中心
汽车制造商在设计汽车时会考虑质心的位置和重心高度,以尽可能减小质心侧偏角。 一些高性能汽车甚至会采用主动悬挂系统来控制车身侧倾,从而减小质心侧偏角,提高车辆的稳定性和操控性。


🎶(G 发动机相关


发动机功率=扭矩转速n

知识百科:说到汽车发动机,要了解几个参数。排量,功率,扭矩,转速。那么这里和参数之间的关系如何,
排量,就是发动机气缸排出气体的多少。因此说到排量,不管四缸,三缸,二缸,一缸,只要大小一样,排量就相同。
功率,单位时间内做功的多少。那么排量越大,单位时间做功就会越多,因此,排量越大,功率也会越大。
扭矩,它的单位是N·M,所以它是力运动单位距离的结果。它反应的是加速度。扭矩越大,加速能力就越强。
转速,它是单位时间内齿轮转动的圈数。齿轮转的越快,传输给轮胎的转速就越高,车子就跑的越快。
在这里插入图片描述

//汽车引擎发动机相关public void CarEnginePower(){WheelRPM();//将轮轴的转速获取// 扭矩力(发动机功率) =  功率=扭矩*转速*nmotorflaot = -enginePowerCurve.Evaluate(engineRPM) * gears[gerrsNurrentNum];float velocity = 0.0f;//发动机的转速 与 车轮转速 和 挡位比率 成比例engineRPM = Mathf.SmoothDamp(engineRPM, 1000 + Mathf.Abs (wheelsRPM) * 3.6f * (gears[gerrsNurrentNum]), ref velocity, smoothTime);print(engineRPM);VerticalContorl();    //驱动管理shifterGearsChange(); //换挡管理}//获得车轮的转速public void WheelRPM(){float sum = 0;for (int i = 0; i < 4; i++){sum += wheels[i].rpm;}//四个车轮轮轴的平均转速wheelsRPM = sum / 4;}//换挡管理public void shifterGearsChange(){if(InputManager.InputManagerment .addGears ) //如果按下E键,加挡{if(gerrsNurrentNum < gears.Length - 1  )gerrsNurrentNum++;}if(InputManager.InputManagerment.lowGears ) //如果按下Q键,减档{if (gerrsNurrentNum > 0)gerrsNurrentNum--;}}

🎶(H 自动挡位变速箱


在这里插入图片描述

不仅仅是发动机牵引着汽车去运动。是发动机跟轮胎一起控制汽车去前进,我们前面没有添加发动机,就是靠轮胎的扭矩力去控制汽车的前进

在这里插入图片描述

怎么来理解自动档位变速箱呢?当发动机。每个档位的发动机。它超过八千转的时候就要换挡了。所以当我们现在设置发动机的最大转是八千,最小转是五千,超过八千转我们就自动加档。小于了五千转,我们就自动减档。——这个最大转和最小转是模拟跑车的。

当然,判断换挡的依据不仅仅是靠超过最大的这个发动机的最大转,连同每个档位设置的那个限速作为一起判断依据。如下图所示,每个档位的限速如果超过了这个限速,并且超过了最大转速,我们就换挡。

在这里插入图片描述

//换挡管理//换挡管理public void shifterGearsChange(){switch (nowGearsType){//档位类型是手动档的时候case EChooseGreas.handMovement:if (InputManager.InputManagerment.addGears) //如果按下E键,加挡{if (gerrsNurrentNum < gears.Length - 1)gerrsNurrentNum++;}if (InputManager.InputManagerment.lowGears) //如果按下Q键,减档{if (gerrsNurrentNum > 0)gerrsNurrentNum--;}break;//档位类型是自动档的时候handMovementcase EChooseGreas.aotomutic://如果车子不在地面不会自动换档if (!IsGrounp()) return;//当发动机转速大于最高转速 并且 速度也超过了相应挡位的限速 ,数组不越界 并且不是倒车 就加档if (engineRPM > maxRPM && Km_H >= gearSpeed[gerrsNurrentNum] && gerrsNurrentNum < gears.Length - 1 && !BackCar())gerrsNurrentNum++;//当发动机小于最小转时 减档 (此时未加判断是因为就是要 更好的减速)if (engineRPM < minRPM && gerrsNurrentNum > 0)gerrsNurrentNum--;break;default:break;}

🅰️系统路线学习点击跳转


⭐【Unityc#专题篇】之c#进阶篇】

⭐【Unityc#专题篇】之c#核心篇】

⭐【Unityc#专题篇】之c#基础篇】

⭐【Unity-c#专题篇】之c#入门篇】

【Unityc#专题篇】—进阶章题单实践练习

⭐【Unityc#专题篇】—基础章题单实践练习

【Unityc#专题篇】—核心章题单实践练习


你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!


在这里插入图片描述


四最终代码

在这里插入图片描述

CarMoveContorl

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:       ______________
//___________功能:  车轮的运动
//___________创建者:_______秩沅________
//_____________________________________
//-------------------------------------//驱动模式的选择
public enum EDriveType
{frontDrive,   //前轮驱动backDrive,    //后轮驱动allDrive      //四驱
}public class CarMoveControl : MonoBehaviour
{//-------------------------------------------//四个轮子的碰撞器public WheelCollider[] wheels ;//网格的获取public GameObject[] wheelMesh;//初始化三维向量和四元数private Vector3 wheelPosition = Vector3.zero;private Quaternion wheelRotation = Quaternion.identity;//-------------------------------------------//驱动模式选择 _默认前驱public EDriveType DriveType = EDriveType.frontDrive;//----------车辆属性特征-----------------------//车刚体public Rigidbody rigidbody;//轮半径public float radius = 0.25f;//扭矩力度public float motorflaot = 8000f;//刹车力public float brakVualue = 800000f;//速度:每小时多少公里public int Km_H;//下压力public float downForceValue = 1000f; //四个轮胎扭矩力的大小public float f_right;public float f_left;public float b_right;public float b_left;//车轮打滑参数识别public float[] slip ;//质心public Vector3 CenterMass;//一些属性的初始化private void Start(){rigidbody = GetComponent<Rigidbody>();slip = new float[4];}private void FixedUpdate(){VerticalAttribute();//车辆物理属性管理WheelsAnimation(); //车轮动画VerticalContorl(); //驱动管理HorizontalContolr(); //转向管理HandbrakControl(); //手刹管理}//车辆物理属性相关public void VerticalAttribute(){//---------------速度实时---------------//1m/s = 3.6km/hKm_H =(int)(rigidbody.velocity.magnitude * 3.6) ;Km_H = Mathf.Clamp( Km_H,0, 200 );   //油门速度为 0 到 200 Km/H之间//--------------扭矩力实时---------------//显示每个轮胎的扭矩f_right = wheels[0].motorTorque;f_left  = wheels[1].motorTorque;b_right = wheels[2].motorTorque;b_left  = wheels[3].motorTorque;//-------------下压力添加-----------------//速度越大,下压力越大,抓地更强rigidbody.AddForce(-transform.up * downForceValue * rigidbody.velocity .magnitude );//-------------质量中心同步----------------//质量中心越贴下,越不容易翻rigidbody.centerOfMass = CenterMass;}//垂直轴方向运动管理(驱动管理)public void VerticalContorl(){switch (DriveType){case EDriveType.frontDrive: //选择前驱if (InputManager.InputManagerment.vertical != 0) //当按下WS键时生效{for (int i = 0; i < wheels.Length - 2; i++){//扭矩力度wheels[i].motorTorque = InputManager.InputManagerment.vertical *(motorflaot / 2); //扭矩马力归半}}else{for (int i = 0; i < wheels.Length - 2; i++){//扭矩力度wheels[i].motorTorque = 0; }}break;case EDriveType.backDrive://选择后驱if (InputManager.InputManagerment.vertical != 0) //当按下WS键时生效{for (int i = 2; i < wheels.Length; i++){//扭矩力度wheels[i].motorTorque = InputManager.InputManagerment.vertical * (motorflaot / 2); //扭矩马力归半}}else{for (int i = 2; i < wheels.Length ; i++){//扭矩力度wheels[i].motorTorque = 0;}}break;case EDriveType.allDrive://选择四驱if (InputManager.InputManagerment.vertical != 0) //当按下WS键时生效{for (int i = 0; i < wheels.Length; i++){//扭矩力度wheels[i].motorTorque = InputManager.InputManagerment.vertical * ( motorflaot / 4 ); //扭矩马力/4}}else{for (int i = 0; i < wheels.Length; i++){//扭矩力度wheels[i].motorTorque = 0;}}break;default:break;}}//水平轴方向运动管理(转向管理)public void HorizontalContolr(){if (InputManager.InputManagerment.horizontal > 0){//后轮距尺寸设置为1.5f ,轴距设置为2.55f ,radius 默认为6,radius 越大旋转的角度看起来越小wheels[0].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius + (1.5f / 2))) * InputManager.InputManagerment.horizontal;wheels[1].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius - (1.5f / 2))) * InputManager.InputManagerment.horizontal;}else if (InputManager.InputManagerment.horizontal < 0){wheels[0].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius - (1.5f / 2))) * InputManager.InputManagerment.horizontal;wheels[1].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius + (1.5f / 2))) * InputManager.InputManagerment.horizontal;}else{wheels[0].steerAngle = 0;wheels[1].steerAngle = 0;}}//手刹管理public void HandbrakControl(){if(InputManager.InputManagerment .handbanl ){//后轮刹车wheels[2].brakeTorque  = brakVualue;wheels[3].brakeTorque  = brakVualue;}else{wheels[2].brakeTorque = 0;wheels[3].brakeTorque = 0;}//------------刹车效果平滑度显示------------for (int i = 0; i < slip.Length; i++){WheelHit wheelhit;wheels[i].GetGroundHit(out wheelhit);slip[i] = wheelhit.forwardSlip; //轮胎在滚动方向上打滑。加速滑移为负,制动滑为正}}//车轮动画相关public  void WheelsAnimation(){for (int i = 0; i < wheels.Length ; i++){//获取当前空间的车轮位置 和 角度wheels[i].GetWorldPose(out wheelPosition, out wheelRotation);//赋值给wheelMesh[i].transform.position = wheelPosition;wheelMesh[i].transform.rotation = wheelRotation * Quaternion .AngleAxis (90,Vector3 .forward );}}
}

CameraFllow

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:       ______________
//___________功能: 相机的跟随
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class CameraFllow : MonoBehaviour
{//目标物体public Transform target;private CarMoveControl Control;public int  speed;//鼠标滑轮的速度public float ScrollSpeed = 45f;//Y轴差距参数public float Ydictance = 0f; public float  Ymin = 0f;public float  Ymax  = 4f;//Z轴差距参数public float Zdictance = 4f;public float Zmin = 4f;public float Zmax = 8f;//相机看向的角度 和最終位置public float angle = -25 ;public Vector3 lookPosition;void LateUpdate(){//Z轴和Y轴的距离和鼠标滑轮联系Ydictance += Input.GetAxis("Mouse ScrollWheel") * ScrollSpeed * Time.deltaTime;//平滑效果Zdictance += Input.GetAxis("Mouse ScrollWheel") * ScrollSpeed * Time.deltaTime;//設置Y軸和x轴的滚轮滑动范围 Ydictance = Mathf.Clamp(Ydictance , Ymin ,Ymax )  ; Zdictance = Mathf.Clamp(Zdictance , Zmin, Zmax )  ;//确定好角度,四元数 * 三维向量 = 三维向量lookPosition = Quaternion.AngleAxis(angle, target .right) * -target.forward ;//更新位置transform.position = target.position + Vector3.up * Ydictance - lookPosition  * Zdictance  ;//更新角度transform.rotation = Quaternion.LookRotation(lookPosition);//实时速度Control = target.GetComponent<CarMoveControl>();speed = (int )Control.Km_H / 4;speed = Mathf.Clamp(speed,0, 55 );   //对应最大200公里每小时}
}

InputMana

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:       ______________
//___________功能: 输入控制管理器
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class InputManager : MonoBehaviour
{//单例模式管理static private InputManager inputManagerment;static public InputManager InputManagerment => inputManagerment;public float horizontal;  //水平方向动力值public float vertical;    //垂直方向动力值public bool  handbanl;    //手刹动力值void Awake(){inputManagerment = this;}void Update(){//与Unity中输入管理器的值相互对应horizontal = Input.GetAxis("Horizontal");vertical = Input.GetAxis("Vertical");handbanl = Input.GetAxis("Jump")!= 0 ? true :false ; //按下空格键时就是1,否则为0}
}
http://www.lryc.cn/news/148723.html

相关文章:

  • 项目-IM
  • 2023年口腔医疗行业研究报告
  • 1Java:JDK是什么
  • 使用 Amazon Lambda 进行无服务器计算:云架构中的一场革命
  • 谨慎使用Lombok的@Builder注解
  • leetcode455. 分发饼干 【贪心】
  • 4V-28V Vin,6A同步降压DCDC变换器,集成3.3V和150mA LDO——SCT2361FPBR
  • Linux中的scp指令
  • 剑指 Offer 11. 旋转数组的最小数字
  • Redis面试题总结
  • 【Eclipse】搭建python环境;运行第一个python程序helloword
  • OpenAI 发布企业版ChatGPT-4
  • Flowable7 设计器
  • Flutter问题记录 - Unable to find bundled Java version
  • Tomcat 日志乱码问题解决
  • yum源以及rpm安装包配置、yum源冲突、yum-config-manager命令找不到、curl: (35)、docker镜像重复拉取失败
  • ChatGPT和文心一言的优缺点比较
  • ⛳ 面试题-单例模式会存在线程安全问题吗?
  • C - 滑动窗口 /【模板】单调队列
  • 工厂人员作业行为动作识别检测算法
  • 【数据结构】顺序表详解
  • HTML 播放器效果
  • C++常用23种设计模式总结(三)------装饰模式
  • 选择O型圈时要考虑哪些因素?
  • 安全管理中心技术测评要求项
  • Hibernate(Spring Data)抓取策略
  • 【高阶数据结构】map和set的介绍和使用 {关联式容器;键值对;map和set;multimap和multiset;OJ练习}
  • 系统架构技能之设计模式-单件模式
  • Redis进阶 - JVM进程缓存
  • SD-WAN带您告别高成本、单一功能和安全性差