DoodleJump


一、 介绍

在这里插入图片描述

知识点:
瓦片地图
如何做到不管从多高落下,回弹速度相同
边缘碰撞器
平台碰撞器
枚举
速度法向量函数collision.contacts[0].normal
Lerp函数
SmoothDamp函数
动画制作
对象池

二、 平台

两种平台,第一种跳跃一次就销毁(播放动画)
第二种可以一直跳跃
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Platform : MonoBehaviour
{
    public PlatformType platformType; // 定义一个公共枚举类型变量 platformType,用于指定平台的类型
    public float bounceSpeed = 4f; // 定义一个公共浮点数类型变量 bounceSpeed,用于设置弹跳速度

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.contacts[0].normal == Vector2.down) // 如果碰撞发生在平台的上方
        {
            Rigidbody2D rb = collision.gameObject.GetComponent<Rigidbody2D>(); // 获取碰撞物体的刚体组件
            if(rb != null) // 如果碰撞物体有刚体组件
            {
                rb.velocity = Vector2.up * bounceSpeed; // 设置碰撞物体在竖直方向上的速度为 bounceSpeed,使其产生一个向上的弹跳效果
            }

            //Weak Platform
            if(platformType == PlatformType.weak) // 如果平台的类型是弱平台
            {
                if(GetComponent<Animator>() != null) // 如果平台上有 Animator 组件
                {
                    GetComponent<Animator>().SetTrigger("Trigger"); // 播放 Animator 中名为 "Trigger" 的动画
                    Invoke("HideGameObject", 0.4f); // 在 0.4 秒后隐藏平台的游戏对象
                }
            }
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if(collision.CompareTag("MainCamera")) // 如果碰撞体的标签是 "MainCamera"
        {
            gameObject.SetActive(false); // 隐藏平台的游戏对象
        }
    }

    void HideGameObject()
    {
        gameObject.SetActive(false); // 隐藏平台的游戏对象
    }
}

public enum PlatformType
{
    normal, weak // 定义一个平台类型的枚举,包括普通平台和弱平台
}

}

三、玩家

左右控制行走、转向
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Doodler : MonoBehaviour
{
    public float moveSpeed;
    Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(h * moveSpeed, rb.velocity.y);

        if(h != 0)
        {
            transform.localScale = new Vector3(-h, 1, 1);
        }
    }
    
  
}


四、 换边儿

左边进去,右边出来。
右边进去,左边出来
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeSide : MonoBehaviour
{
    private void OnTriggerExit2D(Collider2D collision)
    {
        Transform t = collision.gameObject.transform;
        t.position = new Vector3((-t.position.x) / 0.95f, t.position.y, 0f);
    }
}


五、 相机视角

相机跟随玩家移动,使用SmoothDamp函数
摄像机添加碰撞体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform target; // 定义一个公共 Transform 类型变量 target,用于指定摄像机跟随的目标物体
    public float smoothSpeed = 0.3f; // 定义一个公共浮点数类型变量 smoothSpeed,用于设置摄像机跟随的平滑度
    Vector3 speed; // 定义一个私有 Vector3 类型变量 speed,用于记录摄像机跟随的速度

    private void LateUpdate()
    {
        if(target.position.y > transform.position.y) // 如果目标物体的位置在摄像机的上方
        {
            Vector3 targetPos = new Vector3(0f, target.position.y, -10f); // 构造一个新的目标位置,使其在目标物体的上方,同时保持 Z 轴不变
            transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref speed, smoothSpeed * Time.deltaTime); // 使用 SmoothDamp 函数平滑地将摄像机的位置移动到目标位置
        }
    }
}


六、 对象池

生成挡板,防止频繁销毁创建、销毁占用系统资源
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelGenerator : MonoBehaviour
{
    public GameObject[] platformPrefabs; // 定义一个公共 GameObject 数组 platformPrefabs,用于存储平台的预制体
    float currentYPos = 0f; // 定义一个浮点数类型变量 currentYPos,用于记录当前平台的Y坐标
    public float cameraHeight = 5.5f; // 定义一个公共浮点数类型变量 cameraHeight,用于设置相机视野的高度

    public Transform platformPool; // 定义一个公共 Transform 类型变量 platformPool,用于存储平台对象池的父物体

    // Start is called before the first frame update
    void Start()
    {
        SpawnPlatformPool(); // 在场景中生成一定数量的平台对象

        while(currentYPos < Camera.main.transform.position.y + cameraHeight) // 当当前平台的Y坐标小于相机位置加上视野高度时
        {
            PickNewPlatform(); // 生成新的平台
        }
    }

    void SpawnPlatformPool()
    {
        int basicPlatformAmount = 30; // 定义基础平台的数量
        int weakPlatformAmount = 15; // 定义弱平台的数量

        for (int i = 0; i < basicPlatformAmount; i++) // 循环生成基础平台
        {
            GameObject platform = Instantiate(platformPrefabs[0], platformPool); // 实例化基础平台预制体
            platform.SetActive(false); // 将平台隐藏
        }

        for (int i = 0; i < weakPlatformAmount; i++) // 循环生成弱平台
        {
            GameObject platform = Instantiate(platformPrefabs[1], platformPool); // 实例化弱平台预制体
            platform.SetActive(false); // 将平台隐藏
        }
    }

    void PickNewPlatform()
    {
        currentYPos += Random.Range(0.3f, 1f); // 在一定范围内随机生成新的平台Y坐标
        float xPos = Random.Range(-3.8f, 3.8f); // 在一定范围内随机生成新的平台X坐标

        int r = 0;
        do
        {
            r = Random.Range(0, platformPool.childCount); // 在平台对象池中随机选择一个未激活的平台
        } while (platformPool.GetChild(r).gameObject.activeInHierarchy); // 循环直到选择到未激活的平台

        platformPool.GetChild(r).position = new Vector2(xPos, currentYPos); // 将平台移动到新的坐标
        platformPool.GetChild(r).gameObject.SetActive(true); // 激活平台
    }

    // Update is called once per frame
    void Update()
    {
        if(currentYPos < Camera.main.transform.position.y + cameraHeight) // 当当前平台的Y坐标小于相机位置加上视野高度时
        {
            PickNewPlatform(); // 生成新的平台
        }
    }
}

六、 下载工程文件

https://wwez.lanzoul.com/iUBpf0tdo7ih

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忽然602

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值