• [Unity] 制作游戏 赛车小游戏


    模拟赛车小游戏

    效果展示

    请添加图片描述
    做一款模拟赛车的小游戏demo,方向键控制、有刹车、重玩,还有漂移和查看车型的功能
    可以看到,还有翻车的效果哈哈哈!有兴趣的小伙伴来看看吧!

    资源准备

    1. 从网上下载赛车模型和桥梁路的模型
    2. 项目下载链接在文章末尾

    赛车模型如下:请添加图片描述
    然后将桥和路摆放入游戏场景
    请添加图片描述
    再把车放入适当的位置,加入刚体(将刚体mess调节到1000)和车轮碰撞器,层级结构如下:
    请添加图片描述
    车轮碰撞器如下操作,在车内部中新建空节点,然后再建四个空节点,给每个这四个空节点都加上Wheel Collider车轮碰撞器,然后将这四个车轮节点的位置逐个摆放到车子车轮对应的位置,最后稍微调节一下车轮的Friction参数即可。
    在这里插入图片描述

    最后再添加几个button在ui上,层级如下:
    请添加图片描述
    请添加图片描述

    代码阶段

    然后将车挂上脚本lanBoCar.cs
    首先写入要在编辑器中配置的所有public变量

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    
    public class lanBoCar : MonoBehaviour
    {
        public float angleSpeed;
        public float moveSpeed;
        public WheelCollider[] wheels;
        public Transform[] modelTrs;
        public Button[] contrKeyPad;
        public GameObject camera;
        public Button reset;
        public GameObject objbody;
        public GameObject cam;
        public Button LOOK;
        ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    请添加图片描述
    然后写相机镜头锁定车的脚本
    camera.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class camera : MonoBehaviour
    {
        public GameObject pos;
        private Vector3 offset;
        public GameObject playerCar;
        public GameObject ca;
    
        private int lookid = 0;
    
        public int canRot = 1;
        void Start()
        {
            offset = transform.position - playerCar.transform.position;
    
            transform.position = ca.transform.position;
            transform.localEulerAngles = ca.transform.localEulerAngles;
        }
    
        void Update()
        {
            if (lookid == 1)
            {   
                Vector3 pPos = playerCar.transform.position;
                pPos.z -= 4f;
                pPos.y += 1.5f;
                transform.position = pPos;
                transform.localEulerAngles = ca.transform.localEulerAngles;
            }
            else
            {
                transform.position = ca.transform.position;
                float y = playerCar.transform.localEulerAngles.y;
                float z = playerCar.transform.localEulerAngles.z;
                if (canRot == 0)
                {
                    transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, y, z);
                }
                else
                {
                    transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, y + (Time.deltaTime * 10), z);
                }
            }
        }
    
        void changeRot(string v)
        {
            if (v == "stop")
            {
                canRot = 0;
            }
            if (v == "begin")
            {
                canRot = 1;
            }
        }
    
        void look(int ids)
        {
            lookid = ids;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    再写上操作方向键的上下左右button的脚本
    button.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class button : MonoBehaviour,IPointerDownHandler,IPointerUpHandler
    {   
        public float[] vh;
        public GameObject car;
    
        public void OnPointerDown(PointerEventData ed)
        {
            string chec = "";
    
            if (vh.Length == 1 && vh[0] == 2)
            {   
                int idx = 1;
                car.SendMessage("checkTail", idx);
                return;
            }
            if (vh[0] == 1)
            {
                chec = "up";
            }
            if(vh[0] == -1){
                chec = "down";
            }
            if (vh[1] == -1)
            {
                chec = "left";
            }
            if (vh[1] == 1)
            {
                chec = "right";
            }
            car.SendMessage("checkEvent", chec);
        }
    
        public void OnPointerUp(PointerEventData ed)
        {
            string chec = "";
    
            if (vh.Length == 1 && vh[0] == 2)
            {
                int idx = 0;
                car.SendMessage("checkTail", idx);
                return;
            }
            if (vh[0] == 1)
            {
                chec = "uSTOP";
            }
            if (vh[0] == -1)
            {
                chec = "dSTOP";
            }
            if (vh[1] == -1)
            {
                chec = "lSTOP";
            }
            if (vh[1] == 1)
            {
                chec = "rSTOP";
            }
            car.SendMessage("checkEvent", chec);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    然后把这个button.cs挂到每个方向键的组件上

    最后写入车的脚本
    lanBoCar.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    
    public class lanBoCar : MonoBehaviour
    {
        public float angleSpeed;
        public float moveSpeed;
    
        public WheelCollider[] wheels;
        public Transform[] modelTrs;
        public Button[] contrKeyPad;
    
        public GameObject camera;
        public Button reset;
    
        private float h = 0f;
        private float v = 0f;
    
        private float addSpeed = 0f;
        public GameObject objbody;
    
        public GameObject cam;
    
        public Button LOOK;
        private bool lookF = false;
    
        void Start()
        {
            reset.onClick.AddListener(() =>
            {
                SceneManager.LoadScene(0);
            });
    
            LOOK.onClick.AddListener(() =>
            {
                lookF = !lookF;
                int ids;
                if (lookF)
                {
                    ids = 1;
                }
                else
                {
                    ids = 0;
                }
                cam.SendMessage("look",ids);
            });
        }
    
        void FixedUpdate()
        {   
            WheelControl();
        }
    
        void OnMouseDrag()
        {
            if (lookF)
            {
                wheels[2].brakeTorque = 1000f;
                wheels[3].brakeTorque = 1000f;
                transform.Rotate(0f, 40f * Time.deltaTime, 0f);
            }
        }
    
        void WheelControl(){
    
            if (lookF)
            {
                return;
            }
            //h = Input.GetAxis("Horizontal");
            //v = Input.GetAxis("Vertical");
    
            if (addSpeed == 1f)
            {
                Debug.Log("stop");
                wheels[2].brakeTorque = 1200f;
                wheels[3].brakeTorque = 1200f;
            }
            else
            {
                wheels[2].brakeTorque = 0f;
                wheels[3].brakeTorque = 0f;            
            }
    
            wheels[0].steerAngle = h * angleSpeed ;
            wheels[1].steerAngle = h * angleSpeed ;
    
            wheels[2].motorTorque = v * moveSpeed ;
            wheels[3].motorTorque = v * moveSpeed ;
    
            for (int i = 0; i < 4; i++)
            {
                ModelWheelShow(modelTrs[i],wheels[i]);
            }
        }
    
        void ModelWheelShow(Transform trans,WheelCollider wheel){
            Vector3 pos;
            Quaternion rot;
            wheel.GetWorldPose(out pos, out rot);
            trans.rotation = rot;
        }
    
        void checkEvent(string move)
        {
            Debug.Log(move);
            switch (move)
            {
                case "up":
                    v = 1;
                    break;
                case "down":
                    v = -1;
                    break;
                case "left":
                    h = -1;
                    cam.SendMessage("changeRot", "stop");
                    break;
                case "right":
                    cam.SendMessage("changeRot", "stop");
                    h = 1;
                    break;
                case "uSTOP":
                case "dSTOP":
                    v = 0;
                    break;
                case "lSTOP":
                case "rSTOP":
                    cam.SendMessage("changeRot", "begin");
                    h = 0;
                    break;
            }
        }
    
        void checkTail(int id)
        {
            if (id == 1)
            {
                addSpeed = 1f;
            }
            if (id == 0)
            {
                addSpeed = 0f;
            }
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152

    游戏结果

    请添加图片描述
    请添加图片描述

    项目下载

    最后项目下载链接:
    https://download.csdn.net/download/qq_25755645/86265905

  • 相关阅读:
    Docker | 制作tomcat镜像并部署项目
    华为OD机试 - 数字排列(Java & JS & Python & C & C++)
    【IMX6ULL笔记】-- 从驱动到应用(基于Qt)- 串口
    老板:公司系统太多,能不能实现账号互通?
    element-tree树结构-默认选中第一个节点高亮-根据id选中节点高亮
    提升市场调研和竞品分析效率:利用Appium实现App数据爬取
    Linux系统编程-进程间通信(用时一周总结,非常详细,建议收藏)
    第01章 Tableau数据可视化概述
    智慧公厕实现公共厕所全方位“上云用数赋智”根本之道
    Flutter的Don‘t use ‘BuildContext‘s across async gaps警告解决方法
  • 原文地址:https://blog.csdn.net/qq_25755645/article/details/126055702