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

这篇具有很好参考价值的文章主要介绍了Unity 3D汽车模拟驾驶期末大作业。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

  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

一登陆注册场景

  • 有密码登录面板,注册账号面板,设置面板
    Unity,3D汽车模拟,驾驶模拟,期末大作业,Unity代码
    Unity,3D汽车模拟,驾驶模拟,期末大作业,Unity代码
    Unity,3D汽车模拟,驾驶模拟,期末大作业,Unity代码
    代码:

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 update
    void 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 frame
    void Update()
    {
        au.mute = toggle.isOn;
        au.volume = slider.value;
    }}

二加载场景

Unity,3D汽车模拟,驾驶模拟,期末大作业,Unity代码
代码:

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 update
    void Start()
    {
        slider = GetComponent<Slider>();
        text = text.GetComponent<Text>();
        text.text = "";
        slider.value = 0;
    }

    // Update is called once per frame
    void 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);
        }
    }}

三选择场景

Unity,3D汽车模拟,驾驶模拟,期末大作业,Unity代码
场景转换和转换效果代码:

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 update
    void Start()
    {
        au = GetComponent<AudioSource>();
    }
    public void PlayAudio(int num)
    {
        au.clip = clip[num];
        au.Play();
    }
    // Update is called once per frame
    void 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 update
    void Start()
    {
        
    }

    // Update is called once per frame
    void 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 update
    void Start()
    {
        text_timeSpeed = GetComponent<Text>();
    }

    // Update is called once per frame
    void 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
工程文件已上传,自取
包括所有素材源代码。打开即可运行。文章来源地址https://www.toymoban.com/news/detail-401000.html

到了这里,关于Unity 3D汽车模拟驾驶期末大作业的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【自动驾驶汽车量子群粒子过滤器】用于无人驾驶汽车列车定位的量子粒子滤波研究(Matlab代码实现)

    💥💥💞💞 欢迎来到本博客 ❤️❤️💥💥 🏆博主优势: 🌞🌞🌞 博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️ 座右铭: 行百里者,半于九十。 📋📋📋 本文目录如下: 🎁🎁🎁 目录 💥1 概述 📚2 运行结果 🎉3 参考文献 🌈4 Matlab代码实现 对于无人

    2024年02月15日
    浏览(32)
  • Python综合练习:期末大作业使用openpyxl进行模拟学生宿舍管理系统设计与开发

    1.1 问题背景 随着办公智能化的发展,为方便对大学生宿舍的动态管理,宿舍管理系统储存了每个宿舍学生的基本个人信息,同时需要针对一些特殊情况,如转专业、退学等,对宿舍的信息实现动态调整,支持显示、增加、删除、修改、查询成员信息,从而实现宿舍管理员对

    2024年02月07日
    浏览(48)
  • gl-opendrive插件(车俩3D仿真模拟自动驾驶)

    本插件基于免费opendrive开源插件、Threejs和Webgl三维技术、vue前端框架,blender开源建模工具等进行二次开发。该插件由本人独立开发以及负责,目前处于demo阶段,功能还需待完善,由于开发仓促代码还需优化。 两个版本: 1.vue版本pc端可视化展示 2.非vue版本支持内嵌到安卓,

    2024年02月08日
    浏览(90)
  • gl-opendrive插件(车俩3D仿真模拟自动驾驶)

    本插件基于免费opendrive开源插件、Threejs和Webgl三维技术、vue前端框架,blender开源建模工具等进行二次开发。该插件由本人独立开发以及负责,目前处于demo阶段,功能还需待完善,由于开发仓促代码还需优化。 两个版本: 1.vue版本pc端可视化展示 2.非vue版本支持内嵌到安卓,

    2023年04月27日
    浏览(29)
  • 期末大作业图书管理系统(c++)源代码

    功能展示 运行效果 : 主界面: 图书管理界面: 读者管理界面: 借还书管理界面:

    2024年02月11日
    浏览(40)
  • final-期末大作业-制作AR射箭小游戏(Unity AR配置详细教程)

    链接: github仓库 bilibili视频 大作业要求: 制作一款特定技术应用小游戏,并提交技术报告。 内容(请参考以下技术主题,但不限于这些主题): 运用手机拍若干全景图,贴到天空盒或球型天空,做一个简单校园漫游功能。 粒子系统效果制作,必须带一个控制组件,控制粒子

    2024年02月06日
    浏览(34)
  • 合肥工业大学机器视觉期末复习 课件梳理(穿插作业中的伪代码)

    第一部分:低层次视觉 1、滤波器 2、梯度—边缘;梯度—能量(线裁剪) 3、模板匹配;二值图像分析 4、纹理 第二部分:中层次视觉 5、霍夫变换 6、分割 7、局部不变特征——检测、描述和匹配 8、立体 第三部分:高层次的视觉 9、实例识别 10、监督分类的对象检测 11、支持向

    2024年02月02日
    浏览(38)
  • 安卓期末大作业(AndroidStudio开发),垃圾分类app,代码有注释,能正常运行

    安卓期末大作业  app使用的是sqlite数据库,使用的核心类及其组件:Base Adapter,Fragment,View Pager,Alert Dialog.Builder,Option,Animation Draw able(关键帧动画),Media Player(视频),Count Down Timer(倒计时 广告页用),Spinner等 该分类助手的功能是管理员先登录进入后台界面,将数据

    2024年02月12日
    浏览(35)
  • unity期末作业-两个简单小游戏游戏-躲避障碍和跑酷(附下载链接和gif动态图演示)

    游戏角色为一个小人,天上不时会掉落障碍物,人物撞到了会掉生命值,人物可以左右移动跳跃来躲避,带游戏音效,比较简单!具体情况如下所示: 点我下载源文件和exe导出文件》》》》》》》 角色可以上下左右移动,J发射子弹k跳跃,只有在跳板上才可以跳跃,可以吃能

    2024年02月04日
    浏览(50)
  • unity3d在汽车邻域应用浅谈

    Unity3D是一款实时3D内容创作软件,它在汽车应用创新方面发挥了重要作用。以下是Unity3D在汽车应用创新方面的几个关键领域: 智能座舱:Unity3D为汽车智能座舱提供了解决方案,通过重新定义座舱设计,将车舱打造成全新的“第三空间”。这种解决方案不仅提供了炫目的视效

    2024年01月24日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包