unity--期末大作业--3D汽车模拟驾驶

news/2025/2/1 7:01:12/

文章目录

  • 前言
  • 录屏
    • 一登陆注册场景
    • 二加载场景
    • 三选择场景
    • 环岛场景
    • 科目二场景
  • 总结


前言

  1. 这个学期马上就要结束,unity要求做个项目,每到考试周,就喜欢上了黑夜。。。。。。。。。。。。。。。。

  2. 我是做了一个汽车模拟,emmmm…勉强算吧

  3. 总共有六个场景
    3.1 登录注册场景
    3.2 加载场景
    3.3 选择场景
    3.4 迷宫地图场景
    3.5 夜晚道路场景
    3.6 科目二模拟场景

  4. 汽车模型和和晚上的场景是store免费找的,迷宫和科目二模拟是搭的,很简陋,没眼看.

  5. 一些功能都是网上零零碎碎学的然后自己去摸索的.

  6. 总的来说开始很想die,到后面慢慢就熟悉了,依然想die

  7. 最后做出来还是挺…不错的.


录屏

Automobile simulation

一登陆注册场景

  • 有密码登录面板,注册账号面板,设置面板
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class LoginTest : MonoBehaviour
{public InputField user;public InputField pass;public InputField r_User;public InputField r_Pass;public InputField r_CPass;public Text text;public GameObject registerPanel;public GameObject settingPanel;public Toggle toggle;public Slider slider;string rUser = "";string rPass = "";string rCPass = "";AudioSource au;// Start is called before the first frame updatevoid Start(){user = user.GetComponent<InputField>();pass = pass.GetComponent<InputField>();text = text.GetComponent<Text>();r_User = r_User.GetComponent<InputField>();r_CPass = r_CPass.GetComponent<InputField>();r_Pass = r_Pass.GetComponent<InputField>();toggle = toggle.GetComponent<Toggle>();slider = slider.GetComponent<Slider>();au = GetComponent<AudioSource>();//user:123456//pass:888888}public void ActiveSettingPanel(){settingPanel.SetActive(true);}public void CloseSettingPanel(){settingPanel.SetActive(false);}public void ActiveRegisterPanel(){registerPanel.SetActive(true);r_User.text = "";r_Pass.text = "";r_CPass.text = "";}public void DisActiveRegisterPanel(){rUser = r_User.text;rPass = r_Pass.text;rCPass = r_CPass.text;if (rUser!=""&&rPass==rCPass&&rPass!=null){registerPanel.SetActive(false);}else{r_User.text = "";r_Pass.text = "";r_CPass.text = "";}}public void CloseRegisterPanel(){registerPanel.SetActive(false);}public void OnLogin(){if (user.text ==rUser&&pass.text==rPass&&pass.text==rCPass&&rPass!=""){print("登录成功");text.text = "账号密码正确,登录成功";Invoke("ClearFailText", 1);text.color = Color.green;Invoke("GotoLoadingScene",1);}else{print("登录失败");user.text = "";pass.text = "";text.text = "账号或密码错误,登录失败";Invoke("ClearFailText",1);text.color = Color.red;}}public void ClearFailText(){text.text = "";}public void GotoLoadingScene(){UnityEngine.SceneManagement.SceneManager.LoadScene(1);//跳转场景}// Update is called once per framevoid Update(){au.mute = toggle.isOn;au.volume = slider.value;}
}

二加载场景

在这里插入图片描述
代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class EnterLoading : MonoBehaviour
{float t = 0;Slider slider;public Text text;// Start is called before the first frame updatevoid Start(){slider = GetComponent<Slider>();text = text.GetComponent<Text>();text.text = "";slider.value = 0;}// Update is called once per framevoid Update(){t += Time.deltaTime;slider.value = t * 10;text.text = (int)slider.value + "%";if (t >= 10){slider.value = 100;text.text = 100 + "%";UnityEngine.SceneManagement.SceneManager.LoadScene(2);}}
}

三选择场景

在这里插入图片描述
场景转换和转换效果代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;public class SceneLoad : MonoBehaviour
{public Image image_Effect;public float speed = 1;void Start(){StartCoroutine(SceneLoadIn());}public void OnClick_Btn_LoadScene_02(string SceneName){StartCoroutine(SceneLoadOut(SceneName));}//淡出IEnumerator SceneLoadOut(string SceneName){Color tempColor = image_Effect.color;tempColor.a = 0;image_Effect.color = tempColor;while (image_Effect.color.a < 1){//Time.deltaTime 指的是当前这一帧。image_Effect.color += new Color(0, 0, 0, speed * Time.deltaTime);yield return null;}SceneManager.LoadScene(SceneName);}//淡入IEnumerator SceneLoadIn(){Color temColor = image_Effect.color;temColor.a = 0;image_Effect.color = temColor;while (image_Effect.color.a > 0){image_Effect.color += new Color(0, 0, 0, -speed * Time.deltaTime);yield return null;}}}

环岛场景

  1. 摄像机跟着汽车代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CameraFollow : MonoBehaviour
{[SerializeField] private Vector3 offset;[SerializeField] private Transform target;[SerializeField] private float translateSpeed;[SerializeField] private float rotationSpeed;private void FixedUpdate(){HandleTranslation();HandleRotation();}private void HandleTranslation(){var targetPosition = target.TransformPoint(offset);transform.position = Vector3.Lerp(transform.position, targetPosition, translateSpeed * Time.deltaTime);}private void HandleRotation(){var direction = target.position - transform.position;var rotation = Quaternion.LookRotation(direction, Vector3.up);transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);}
}
  1. 汽车加速刹车等声音代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class AudioManage : MonoBehaviour
{AudioSource au;public AudioClip[] clip = new AudioClip[4];// Start is called before the first frame updatevoid Start(){au = GetComponent<AudioSource>();}public void PlayAudio(int num){au.clip = clip[num];au.Play();}// Update is called once per framevoid Update(){}
}
  1. 汽车移动控制,车灯效果,加速特效等代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CarController1 : MonoBehaviour
{AudioManage audioManage;[SerializeField] public GameObject text;public GameObject lig;int li=0;[SerializeField] public GameObject effect1;[SerializeField] public GameObject effect2;private const string HORIZONTAL = "Horizontal";private const string VERTICAL = "Vertical";private float HorizontalInput;private float verticalInput;private float currentSteerAngle;private float currentbreakForce;private bool isBreaking;[SerializeField] private float motorForce;[SerializeField] private float breakForce;[SerializeField] private float maxSteerAngle;[SerializeField] private WheelCollider frontLeftWheelCollider;[SerializeField] private WheelCollider frontRightWheelCollider;[SerializeField] private WheelCollider rearLeftWheelCollider;[SerializeField] private WheelCollider rearRightWheelCollider;[SerializeField] private Transform frontLeftWheelTransform;[SerializeField] private Transform frontRightWheelTransform;[SerializeField] private Transform rearLeftWheelTransform;[SerializeField] private Transform rearRightWheelTransform;void Start(){audioManage = GameObject.Find("AudioManage").GetComponent<AudioManage>();}private void FixedUpdate(){//Time.timeScale = 1.0f;//text.SetActive(false);GetInput();HandleMotor();HandleSteering(); UpdateWheels();}private void GetInput(){HorizontalInput = Input.GetAxis(HORIZONTAL);verticalInput = Input.GetAxis(VERTICAL);isBreaking = Input.GetKey(KeyCode.Space);//isShift = Input.GetKey(KeyCode.LeftShift);}private void HandleMotor(){frontLeftWheelCollider.motorTorque = verticalInput * motorForce;frontRightWheelCollider.motorTorque = verticalInput * motorForce;if (Input.GetKeyDown(KeyCode.LeftShift)){audioManage.PlayAudio(0);effect1.SetActive(true);effect2.SetActive(true);frontLeftWheelCollider.motorTorque = 20000000.0f;frontRightWheelCollider.motorTorque = 20000000.0f;}if (Input.GetKey(KeyCode.L)){if (li == 0){lig.SetActive(true);li = 1;}else{lig.SetActive(false);li = 0;}}if (Input.GetKeyDown(KeyCode.X)){audioManage.PlayAudio(1);frontLeftWheelCollider.motorTorque = -20000000.0f;frontRightWheelCollider.motorTorque = -20000000.0f;}if (Input.GetKeyDown(KeyCode.W))audioManage.PlayAudio(3);if (Input.GetKeyDown(KeyCode.S)){audioManage.PlayAudio(2);effect1.SetActive(false);effect2.SetActive(false);}currentbreakForce = isBreaking ? breakForce : 0f;if (isBreaking){ApplyBreaking();}}private void ApplyBreaking(){frontRightWheelCollider.brakeTorque = currentbreakForce;frontLeftWheelCollider.brakeTorque = currentbreakForce;rearRightWheelCollider.brakeTorque = currentbreakForce;rearRightWheelCollider.brakeTorque = currentbreakForce;}private void HandleSteering(){currentSteerAngle = maxSteerAngle * HorizontalInput;frontLeftWheelCollider.steerAngle = currentSteerAngle;frontRightWheelCollider.steerAngle = currentSteerAngle;}private void UpdateWheels(){UpdateSinglewheel(frontLeftWheelCollider, frontLeftWheelTransform);UpdateSinglewheel(frontRightWheelCollider, frontRightWheelTransform);UpdateSinglewheel(rearRightWheelCollider, rearRightWheelTransform);UpdateSinglewheel(rearLeftWheelCollider, rearLeftWheelTransform );}private void UpdateSinglewheel(WheelCollider wheelCollider, Transform wheelTransform){Vector3 pos;Quaternion rot;wheelCollider.GetWorldPose(out pos, out rot);wheelTransform.rotation = rot;wheelTransform.position = pos;}}
  1. 时间代码和计时代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;public class GetTime1 : MonoBehaviour
{public Text TxtCurrentTime;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){DateTime NowTime = DateTime.Now.ToLocalTime();TxtCurrentTime.text = NowTime.ToString("时间:yyyy-MM-dd HH:mm:ss");}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class time : MonoBehaviour
{private int hour;private int minute;private int second;private int millisecond;//已经花费的时间float timeSpeed = 0.0f;//显示时间区域的文本Text text_timeSpeed;// Start is called before the first frame updatevoid Start(){text_timeSpeed = GetComponent<Text>();}// Update is called once per framevoid Update(){timeSpeed += Time.deltaTime;hour = (int)timeSpeed / 3600;minute = ((int)timeSpeed - hour * 3600) / 60;second = (int)timeSpeed - hour * 3600 - minute * 60;//text_timeSpeed.text = string.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second);millisecond = (int)((timeSpeed - (int)timeSpeed) * 1000);text_timeSpeed.text = string.Format("计时:{0:D2}:{1:D2}:{2:D2}.{3:D3}", hour, minute, second, millisecond);}
}

科目二场景

  1. 碰撞检测触碰白线代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class delay : MonoBehaviour
{[SerializeField] public GameObject text;private void OnCollisionEnter(Collision collision){//Time.timeScale = 0;text.SetActive(true);}
}
  1. 进入模拟测试前的白线和结束后的白线代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Tanchuan : MonoBehaviour
{int i = 0;public GameObject text;private void OnCollisionEnter(Collision collision){if (i==0){i = 1;Time.timeScale = 0;text.SetActive(true);}}
}

总结

三个场景里有相同的代码脚本,直接复制就行
这是个团队干的活!!一个人干会累死!


https://download.csdn.net/download/weixin_44954896/85583972?spm=1001.2014.3001.5503
工程文件已上传,自取
包括所有素材源代码。打开即可运行。


http://www.ppmy.cn/news/356859.html

相关文章

新能源汽车迎涨价潮,昂视3D视觉检测降本方案来了

近日&#xff0c;新能源汽车行业迎来了一波涨价热潮。刚刚过去的3月&#xff0c;已经有近20家新能源车企宣布旗下车型涨价&#xff08;消息来源&#xff1a;国际金融报&#xff09;。 新能源汽车分为纯电动汽车、燃料电池电动汽车、增程式电动汽车、 混合动力汽车等。 而燃料电…

衍生式设计+纤维增强3D打印对汽车零部件进行轻量化设计制造

随着新型汽车和新能源汽车的发展&#xff0c;人们对汽车的舒适性和安全性要求逐步提高&#xff0c;同时为了降低汽车能源消耗&#xff0c;有必要在汽车零件开发过程中实现复杂结构零部件的轻量化结构设计。近年&#xff0c;德国奥芬堡应用科学大学的科研人员研究了使用衍生式设…

3D机器视觉在新能源汽车动力电池行业的应用

新能源汽车作为新时代的宠儿&#xff0c;是全世界众多国家战略发展的重点&#xff0c;这也对动力电池提出了更高的要求。随之&#xff0c;电动汽车自燃或存在起火隐患的情况增多&#xff0c;据统计&#xff0c;2020年到目前为止已经出现将近10余起新能源汽车起火事故。为了减少…

Qt 3D 汽车仪表盘

QT3D实现一个汽车仪表盘 一、3D模型的制作1、制作仪表盘模型2、制作汽车模型3、制作仪表指针模型4、其他贴图 二、模型导入Qt中1、Scene3D类型2、 Entity 类型3、SceneLoader{}4、添加灯光5、指针旋转 三、程序运行效果截图四、Qt 3D 资产调节管道1、qgltf介绍&#xff1a;2、q…

智能化汽车3D ToF摄像头

理想L9&#xff08;理想ONE之后的第二款车&#xff09;将搭载3D-ToF传感器&#xff0c;除了驾驶员眼睛注视和头部跟踪监控&#xff0c;还将作为理想自研的深度学习多模态三维空间交互技术的主要硬件载体。 而此前&#xff0c;大部分用于检测驾驶员疲劳的驾驶员监测系统 (DMS)均…

可旋转的汽车3D模型效果的实现

&#xfeff;&#xfeff; 转载请注明出处&#xff1a; http://blog.csdn.net/zhaokaiqiang1992 今天要给大家介绍的是如何实现可旋转的汽车3D模型。 先看实现效果 这只是静态图&#xff0c;实际上&#xff0c;这个模型是可以根据手势进行旋转的&#xff0c;效果还可以。 下面…

基于显扬科技3D机器视觉HY-M5在汽车行业曲轴抓取上下料的应用

行业背景&#xff1a; 曲轴是发动机中最重要的部件。它承受连杆传来的力&#xff0c;并将其转变为转矩通过曲轴输出并驱动发动机上其他附件工作。因此要求曲轴有足够的强度和刚度&#xff0c;轴颈表面需耐磨、工作均匀、平衡性好。传统的曲轴上料靠人力配合简单辅助设备完成&a…

基于Three.js实现3D汽车模型

tips: 运行前请准备好对应的3D模型及Three.js三方库依赖 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><meta http-equiv"X-UA-Compatible" content"IEedge" /><meta name"v…