unity2D跑酷游戏

项目成果 项目网盘

导入资源包 放入Assets文件Assets资源文件

游戏流程分析

摄像机size调小,让图片占满屏幕

人跑本质,相对运动,图片无限向右滚动

图片720,缩小100倍第二个图片x为7.2每unit px100两张图片刚好挨着连贯

空对象BgControl,方便管理

reset

放两张图片

创脚本Bgcontrol,拖到该对象上

层级-10

脚本文件夹,c#脚本

地面对象

层级-5

碰撞组件box collider 2d,编辑碰撞器。

挂脚本

三个地面都拖成预设体

前两个地面固定,后一个随机

Prefabs预设体文件夹

ground拖进去

coin进去

切片

多张

sprite editor

切片

应用

对象声音,挂脚本

单例

创建groundcontrol脚本

脚本挂到地面对象上

预设体相当于类和对象

背景 -10

地面 -5地面预设体给标签ground 碰撞器

声音 挂脚本

动画 窗口 跑 跳 死亡 过渡 设置参数

无退出,过渡0,isjump true

主角 层级10 标签player 碰撞组件 刚体组件 冻结旋转 重力缩放2,只提供重力加速度,加脚本(跳跃,地面上才能跳跃,播放声音,动画切换),

button UI 原图像 原像素显示,过渡 精灵切换 高亮图像 点击 玩家的跳跃 导航none

金币 脚本 碰撞器 吃的触发

桌子 放1号地面上 碰撞器 标签地面 预设体

1地应用所有

死亡 空对象die 碰撞器 编辑触发器 标签die

player脚本

敌人 圆形碰撞器 1地 标签enemy 1地应用所有 预设体 触发

层级

切片

脚本代码

PlayerControl

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
    //血量
    public static int Hp=1;
    //刚体组件
    private Rigidbody2D rbody;
    //动画组件
    private Animator ani;
    private bool isGround;

    void Start()
    {
        //获取刚体组件
        rbody = GetComponent<Rigidbody2D>();
        //获取动画组件
        ani = GetComponent<Animator>();
    }

    void Update()
    {
        //如果按了空格键
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //跳跃
            Jump();
        }
    }

    //跳跃
    public void Jump()
    {
        if (isGround == true)
        {
            //给刚体一个向上的力
            rbody.AddForce(Vector2.up * 400);
            //播放跳跃声音
            AudioManager.Instance.Play("跳");
        }
        
    }
    //发生碰撞
    private void OnCollisionEnter2D(Collision2D collision)
    {
        //判断如果是地面
        if(collision.collider.tag=="Ground")
        {
            isGround = true;
            //结束跳跃
            ani.SetBool("IsJump", false);
        }
        //如果是死亡边界
        if(collision.collider.tag=="Die" && Hp > 0)
        {
            //血量为0
           Hp = 0;
           //

            //播放死亡声音
            AudioManager.Instance.Play("Boss死了");
            //播放死亡动画
            ani.SetBool("IsDie", true);
        }
    }

    //结束碰撞
    private void OnCollisionExit2D(Collision2D collision)
    {
        //判断如果是地面
        
        if (collision.collider.tag == "Ground")
        {
            isGround = false;
            //开始跳跃
            ani.SetBool("IsJump", true);
        }
    }
    //如果碰到敌人
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Enemy")
        {
            //血量为0
            Hp = 0;
            //

            //播放死亡声音
            AudioManager.Instance.Play("Boss死了");
            //播放死亡动画
            ani.SetBool("IsDie", true);
        }
    }
}

CoinControl

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CoinControl : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    //如果产生触发
    private void OnTriggerEnter2D(Collider2D collision)
    {
        //播放吃金币的声音
        AudioManager.Instance.Play("金币");
        //销毁自己
        Destroy(gameObject);
    }
}

AudioMnager

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : MonoBehaviour
{
    //单例
    public static AudioManager Instance;
    //播放组件
    private AudioSource player;

    void Start()
    {
        //单例
        Instance = this;
        //获取播放组件
        player = GetComponent<AudioSource>();
    }

    //播放音效
    public void Play(string name)
    {
        //通过名称获取音频片段
        AudioClip clip = Resources.Load<AudioClip>(name);
        //播放
        player.PlayOneShot(clip);
    }
}

BgControl

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BgControl : MonoBehaviour
{
    //速度 每帧移动0.2像素
    public float Speed = 0.2f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()   //update是每一帧会调用一次
    {
        //如果玩家血量为0
        if(PlayerControl.Hp==0)
        {
            return;
        }
        //遍历背景,背景就是子物体
        foreach (Transform tran in transform)
        {
            //获取子物体的位置
            Vector3 pos = tran.position;
            //按照速度向左侧移动
            pos.x -= Speed * Time.deltaTime;  //每秒向左侧移动0.2
            //判断是否出了屏幕
            if (pos.x < -7.2f)
            {
                //把图片移动到右边
                pos.x += 7.2f * 2;
            }
            //位置赋给子物体
            tran.position = pos;
        }

    }
}

GroundControl

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GroundControl : MonoBehaviour
{//速度
    public float Speed = 2f;
    //要随机的地面数组
    public GameObject[] GroundPrefabs;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()   //update是每一帧会调用一次
    {
        //如果玩家血量为0
        if (PlayerControl.Hp == 0)
        {
            return;
        }
        //遍历背景,背景就是子物体
        foreach (Transform tran in transform)
        {
            //获取子物体的位置
            Vector3 pos = tran.position;
            //按照速度向左侧移动
            pos.x -= Speed * Time.deltaTime;  //每秒向左侧移动0.2
            //判断是否出了屏幕
            if (pos.x < -7.2f)
            {
                //创建新的地面
                Transform newTrans = Instantiate(GroundPrefabs[Random.Range(0, GroundPrefabs.Length)], transform).transform;
// ,后transform确定父子关系   .后transform拿到新地面的transform组件
                //获取新地面的位置
                Vector2 newPos = newTrans.position;
                //设置新地面的位置
                newPos.x = pos.x + 7.2f * 2;
                //位置设置回去
                newTrans.position = newPos;
                //销毁旧的地面(出了屏幕的地面)
                Destroy(tran.gameObject);
            }
            //位置赋给子物体
            tran.position = pos;
        }

    }
}
相关推荐
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹5 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
kybs19915 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
Polevne5 天前
C# SFTP客户端实现文件传输
c#·ssh
samble5 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
cjp5605 天前
024 . WPF MVVM类封装优化
c#
淡海水6 天前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
asdzx676 天前
如何使用 C# 提取 PPT 文本与图片
c#·ppt·提取文本
2501_930707786 天前
使用C#代码使用条件格式为 Excel 隔行设置颜色
c#·excel
何以解忧唯有撸码6 天前
【开源】c#造个轮子-日程安排工具
c#·自定义控件