当前位置:首页 >> 核电技术聚变聚能设备 >> 【Unity3D赛车游戏】【三】如何将汽车进入驱动模式——四驱,二驱转换,3dmark2001

【Unity3D赛车游戏】【三】如何将汽车进入驱动模式——四驱,二驱转换,3dmark2001

cpugpu芯片开发光刻机 核电技术聚变聚能设备 1
文件名:【Unity3D赛车游戏】【三】如何将汽车进入驱动模式——四驱,二驱转换,3dmark2001 【Unity3D赛车游戏】【三】如何将汽车进入驱动模式——四驱,二驱转换


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

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

👨‍💻 本文由 秩沅 原创

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

⭐🅰️Unity3D赛车游戏⭐

文章目录 ⭐🅰️Unity3D赛车游戏⭐⭐前言⭐🎶(==A==)封装管理——输入管理类🎶(==B==)封装管理——模块化管理车辆脚本🎶(==C==)车辆玩法——驱动模式添加😶‍🌫️前驱效果😶‍🌫️后驱效果😶‍🌫️四驱效果😶‍🌫️UNL和代码 ``⭐🅰️⭐


⭐前言⭐

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



🎶(A)封装管理——输入管理类
单例模式管理输入控制类 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 float handbanl; //手刹动力值void Awake(){inputManagerment = this;}void Update(){//与Unity中输入管理器的值相互对应horizontal = Input.GetAxis("Horizontal");vertical = Input.GetAxis("Vertical");handbanl = Input.GetAxis("Space");}}
🎶(B)封装管理——模块化管理车辆脚本
车轮运动脚本优化 using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: ______________//___________功能: 车轮的运动//___________创建者:_______秩沅________//_____________________________________//-------------------------------------public class WheelMove : MonoBehaviour{//-------------------------------------------//四个轮子的碰撞器public WheelCollider[] wheels ;//网格的获取public GameObject[] wheelMesh;//扭矩力度public float motorflaot = 200f;//转向力度public float steerflaot = 20f;//初始化三维向量和四元数private Vector3 wheelPosition = Vector3.zero;private Quaternion wheelRotation = Quaternion.identity;//-------------------------------------------//辅助变量private Vector3 off;private void FixedUpdate(){WheelsAnimation(); //车轮动画VerticalContorl(); //驱动管理HorizontalContolr(); //转向管理}//垂直轴方向管理(驱动管理)public void VerticalContorl(){//垂直轴不为0时if (Input.GetAxis("Vertical") != 0) //当按下WS键时生效{for (int i = 0; i < wheels.Length; i++){//扭矩力度wheels[i].motorTorque = InputManager.InputManagerment.vertical * motorflaot;}}}//水平轴方向管理(转向管理)public void HorizontalContolr(){//水平轴不为0时if (Input.GetAxis("steerAngle") != 0) //当按下AD键时生效{for (int i = 0; i < wheels.Length - 2; i++) //只针对前轮{//转向角度wheels[i].steerAngle = InputManager.InputManagerment.horizontal * steerflaot;}}}//车轮动画相关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 );}}}
🎶(C)车辆玩法——驱动模式添加
车轮参数 😶‍🌫️前驱效果

😶‍🌫️后驱效果

😶‍🌫️四驱效果

按下Gizmos可看到网格的显隐

😶‍🌫️UNL和代码

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;//转向力度public float steerflaot = 20f;//初始化三维向量和四元数private Vector3 wheelPosition = Vector3.zero;private Quaternion wheelRotation = Quaternion.identity;//-------------------------------------------//辅助变量private Vector3 off;//驱动模式选择 _默认前驱public EDriveType DriveType = EDriveType.frontDrive;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(){//水平轴不为0时if (InputManager.InputManagerment.horizontal != 0) //当按下AD键时生效{for (int i = 0; i < wheels.Length - 2; i++) //只针对前轮{//转向角度wheels[i].steerAngle = InputManager.InputManagerment.horizontal * steerflaot;}}}//车轮动画相关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 );}}} ``⭐🅰️⭐

⭐【Unity3D赛车游戏制作】【一】初步导入,资源很哇塞

⭐【Unity3D赛车游戏】【二】如何制作一个真实模拟的汽车

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

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

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

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

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

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

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


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



协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接