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

【Unity3D赛车游戏】【四】在Unity中添加阿克曼转向,下压力,质心会让汽车更稳定

在这里插入图片描述


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

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

👨‍💻 本文由 秩沅 原创

👨‍💻 收录于专栏:Unity游戏demo

🅰️Unity3D赛车游戏



文章目录

    • 🅰️Unity3D赛车游戏
    • 前言
    • 🎶(==A==)车辆优化——阿克曼转向添加
        • 😶‍🌫️认识阿克曼转向
        • 😶‍🌫️区别:
        • 😶‍🌫️关键代码
        • 😶‍🌫️完整代码
    • 🎶(==B==)车辆优化——车身持续稳定的优化
        • 😶‍🌫️速度属性实时转换
        • 😶‍🌫️为车子添加下压力
        • 😶‍🌫️质心的添加centerMess
        • 😶‍🌫️轮胎的平滑度的显示
    • 🅰️


前言


😶‍🌫️版本: Unity2021
😶‍🌫️适合人群:Unity初学者
😶‍🌫️学习目标:3D赛车游戏的基础制作
😶‍🌫️技能掌握:



🎶(A车辆优化——阿克曼转向添加


😶‍🌫️认识阿克曼转向

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

原理图:
_____________在这里插入图片描述
简单理解一个杆子把左轮和右轮连接起来一起转。

在这里插入图片描述
左轮的旋转的半径小于右轮

优点:大大减小了车轮转向需要的空间,转向更加稳定

  • 阿克曼公式:

在这里插入图片描述
β为汽车前外轮转角,α为汽车前内轮转角,K为两主销中心距,L为轴距。

在这里插入图片描述

😶‍🌫️区别:

  • 未添加阿克曼转向之前的原理:

    通过控制轮子的最大转向范围来转向

在这里插入图片描述

  • 添加之后

    更稳定,机动性更强

在这里插入图片描述

😶‍🌫️关键代码

  • 后轮距尺寸设置为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;}

😶‍🌫️完整代码

在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:       ______________
//___________功能:  车轮的运动
//___________创建者:_______秩沅________
//_____________________________________
//-------------------------------------//驱动模式的选择
public enum EDriveType
{frontDrive,   //前轮驱动backDrive,    //后轮驱动allDrive      //四驱
}public class WheelMove : MonoBehaviour
{//-------------------------------------------//四个轮子的碰撞器public WheelCollider[] wheels ;//网格的获取public GameObject[] wheelMesh;//扭矩力度public float motorflaot = 200f;//初始化三维向量和四元数private Vector3 wheelPosition = Vector3.zero;private Quaternion wheelRotation = Quaternion.identity;//-------------------------------------------//驱动模式选择 _默认前驱public EDriveType DriveType = EDriveType.frontDrive;//轮半径public float radius = 0.25f;private void FixedUpdate(){WheelsAnimation(); //车轮动画VerticalContorl(); //驱动管理HorizontalContolr(); //转向管理}//垂直轴方向管理(驱动管理)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); //扭矩马力归半}}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); //扭矩马力归半}}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}}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 WheelsAnimation(){for (int i = 0; i < wheels.Length ; i++){//获取当前空间的车轮位置 和 角度wheels[i].GetWorldPose(out wheelPosition, out wheelRotation);//赋值给wheelMesh[i].transform.position = wheelPosition;print(wheelRotation);wheelMesh[i].transform.rotation = wheelRotation * Quaternion .AngleAxis (90,Vector3 .forward );}}
}}}
}

🎶(B车辆优化——车身持续稳定的优化


WheelMove脚本 ——> CarMoveControl脚本 更改脚本名


😶‍🌫️速度属性实时转换


  • 每小时多少公里 和 每秒多少米的对应关系 ——1m/s = 3.6km/h

速度属性建议改成Int类型 ,float类型会上下浮动不准确

 //1m/s = 3.6km/hKm_H =(int)(rigidbody.velocity.magnitude * 3.6) ;Km_H = Mathf.Clamp( Km_H,0, 200 );   //油门速度为 0 到 200 Km/H之间
  • 相机测速 m/s
    在这里插入图片描述
  //相机监测实时速度Control = target.GetComponent<CarMoveControl>();speed = (int )Control.Km_H / 4;speed = Mathf.Clamp(0, 55,speed );   //对应最大200公里每小时
  • 添加四个轮子的实时速度,对应虚度属性,可以明显的观察四驱和二驱的汽车动力

在这里插入图片描述

    //车辆物理属性相关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 );

😶‍🌫️质心的添加centerMess


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

质量中心越贴下,越不容易翻
在这里插入图片描述

        //-------------质量中心同步----------------//质量中心越贴下,越不容易翻rigidbody.centerOfMass = CenterMass;
  • 手刹的添加
//手刹管理public void HandbrakControl(){if(InputManager.InputManagerment .handbanl ){     //后轮刹车wheels[2].brakeTorque  = brakVualue;wheels[3].brakeTorque  = brakVualue;}else{wheels[2].brakeTorque = 0;wheels[3].brakeTorque = 0;}}

😶‍🌫️轮胎的平滑度的显示


wheelhit.forwardSlip;用来观看刹车轮胎在滚动方向上打滑。加速滑移为负,制动滑为正
_______在这里插入图片描述

for (int i = 0; i < slip.Length; i++){WheelHit wheelhit;wheels[i].GetGroundHit(out wheelhit);slip[i] = wheelhit.forwardSlip; //轮胎在滚动方向上打滑。加速滑移为负,制动滑为正}               

🅰️

在这里插入图片描述
在这里插入图片描述


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

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

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

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

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

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

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


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


在这里插入图片描述


http://www.lryc.cn/news/138341.html

相关文章:

  • Python爬虫requests判断请求超时并重新post/get发送请求
  • CSS中如何实现多列布局?
  • 【C++】string简单实用详解
  • opencv 进阶16-基于FAST特征和BRIEF描述符的ORB(图像匹配)
  • Unity 类Scene窗口相机控制
  • juc基础(三)
  • c语言函数指针和指针函数的区别,以及回调函数的使用。
  • 什么是服务端渲染?前后端分离的优点和缺点?
  • 【Java】优化重复冗余代码的8种方式
  • rabbitmq卸载重新安装3.8版本
  • MyBatis分页思想和特殊字符
  • 设计模式大白话——命令模式
  • [线程/C++(11)]线程池
  • VR防地质灾害安全教育:增强自然灾害知识,提高自我保护意识
  • Mybatis多对多查询案例!
  • Android OpenCV(七十五): 看看刚”转正“的条形码识别
  • 数据结构——布隆计算器
  • 金融学复习博迪(第6-9章)
  • 解决idea登录github copilot报错问题
  • 什么是Flex布局?请列举一些Flex布局的常用属性。
  • React + TypeScript + antd 常见开发场景
  • 前端基础踩坑记录
  • k8s删除pod镜像没响应marking for deletion pod TaintManagerEviction
  • Nginx 使用 lua-nginx-module 来获取post请求中的request和response信息
  • 【Opencv】三维重建之cv::recoverPose()函数(1)
  • Perl兼容正则表达式函数-PHP8知识详解
  • Python处理空值NaN
  • 软件机器人助力交通运输局数据录入,实现高效管理
  • 时序分解 | MATLAB实现基于SGMD辛几何模态分解的信号分解分量可视化
  • FinalShell报错:Swap file “.docker-compose.yml.swp“ already exists