企业整站优化,专业知识,一个公司的网站怎么做的,大数据和网站建设#x1f468;#x1f4bb;个人主页#xff1a;元宇宙-秩沅
#x1f468;#x1f4bb; hallo 欢迎 点赞#x1f44d; 收藏⭐ 留言#x1f4dd; 加关注✅!
#x1f468;#x1f4bb; 本文由 秩沅 原创
#x1f468;#x1f4bb; 收录于专栏#xff1a;uni… 个人主页元宇宙-秩沅
hallo 欢迎 点赞 收藏⭐ 留言 加关注✅!
本文由 秩沅 原创
收录于专栏unity每日一记
⭐️推荐文章⭐ ⭐【软件设计师高频考点暴击】
⭐【Unityc#专题篇】之c#系统化大礼包】
⭐【unity数据持久化】数据管理类_PlayerPrfs
⭐【unity本站最全系列】unity常用API大全一篇文章足以 ⭐WheelColiderz组件汽车游戏的关键⭐ 文章目录 ⭐️推荐文章⭐⭐WheelColiderz组件汽车游戏的关键⭐A 关键API知识B 参数一览C 扭矩力D 阿克曼转向E 汽车下压力 F 汽车质心 G 发动机相关 H 自动挡位变速箱 ⭐️系统路线学习点击跳转⭐ 四最终代码CarMoveContorlCameraFllowInputMana 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 默认为6radius 越大旋转的角度看起来越小 if (horizontal 0 ) {
//后轮距尺寸设置为1.5f 轴距设置为2.55f radius 默认为6radius 越大旋转的角度看起来越小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 GetComponentRigidbody();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 默认为6radius 越大旋转的角度看起来越小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.GetComponentCarMoveControl();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}
}