当前位置: 首页 > news >正文

Unity教学 项目2 2D闯关游戏

视频教程:

https://www.bilibili.com/video/BV1mjXsYXERe?spm_id_from=333.788.videopod.sections&vd_source=25b783f5f945c4507229e9dec657b5bb

1.基本设置

1.1 素材

创建项目

调整布局

基础设置

导入素材

设置人物图片

“过滤模式”三个选项的区别

属性 Point (No Filter) Bilinear Trilinear
插值方式 最近邻插值(Nearest Neighbor) 双线性插值(4 像素加权平均) 三线性插值(Mipmap 层级间平滑过渡)
清晰度 高(像素化明显) 中等(轻微模糊) 中等(更柔和的模糊)
锯齿效果 明显 减少 减少
模糊程度 轻微 更高
性能开销 最低 中等 较高
适用场景 像素风格游戏、保持原始像素感的纹理 普通 3D 游戏中的纹理、UI 元素 远距离观察的物体(如地形、远景)
Mipmap 支持 不支持平滑过渡 支持单层 Mipmap 平滑 支持多层 Mipmap 平滑过渡
视觉效果特点 锐利、块状效果 平滑、自然 过渡自然、适合远近视角切换

1.2 场景、叠层

1.2.1 场景

设置森林背景图片

显示“平铺调色盘”

创建瓦片地图相关的文件夹、文件

绘制地图

1.2.2 叠层

新增三个图层

调色板可增加功能按钮

新建瓦片地图

依次设置排序图层

依次设置图层顺序

修改摄像机背景颜色

1.3 规律瓦片

新建文件夹、动画瓦片、规律瓦片

设置规律瓦片规则

设置动画瓦片规则

最终效果

2.玩家

2.1 碰撞体、物理组件

地面

玩家

2.2 输入系统

设置新的输入系统

安装“Input System”包

创建输入系统脚本

2.3 移动翻转

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;public class PlayerController : MonoBehaviour
{[Header("基础参数")]public Vector2 inputDirection;public float speed;public float jumpForce;public PlayerInputContoller inputController;private Rigidbody2D rb;private bool isGrounded;private void Awake(){inputController = new PlayerInputContoller();rb = GetComponent<Rigidbody2D>();}// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){inputDirection = inputController.Player.Move.ReadValue<Vector2>();}private void FixedUpdate(){Move();}private void OnEnable(){inputController.Enable();}private void OnDisable(){inputController.Disable();}void Move(){rb.velocity = new Vector2(inputDirection.x * speed * Time.deltaTime, rb.velocity.y);float faceDir = transform.localScale.x;if(inputDirection.x < 0) faceDir = -1;if(inputDirection.x > 0) faceDir = 1;transform.localScale = new Vector3(faceDir, 1, 1);}
}

2.4 跳跃

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;public class PlayerController : MonoBehaviour
{public Vector2 inputDirection;public float speed;public float jumpForce;public LayerMask groundLayer;public Transform groundCheck;public float groundCheckRadius = 0.2f;public PlayerInputContoller inputController;private Rigidbody2D rb;private bool isGrounded;private void Awake(){inputController = new PlayerInputContoller();rb = GetComponent<Rigidbody2D>();inputController.Player.Jump.started += Jump;}// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){inputDirection = inputController.Player.Move.ReadValue<Vector2>();}private void FixedUpdate(){Move();CheckGround();}private void OnEnable(){inputController.Enable();}private void OnDisable(){inputController.Disable();}void Move(){rb.velocity = new Vector2(inputDirection.x * speed * Time.deltaTime, rb.velocity.y);float faceDir = transform.localScale.x;if(inputDirection.x < 0) faceDir = -1;if(inputDirection.x > 0) faceDir = 1;transform.localScale = new Vector3(faceDir, 1, 1);}private void Jump(InputAction.CallbackContext context){if (isGrounded){Debug.Log("Jumping");rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);isGrounded = false;}else {Debug.Log("Not Jumping, not grounded");}}   private void CheckGround(){isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);}
}

2.5 闲置、移动动画

添加动画控制器、动画片段

设置动画器

代码

2.6 跳跃动画

2.7 受伤

Player

Boar

using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
/// 角色基础属性管理类,处理生命值和伤害计算
/// </summary>
public class Character : MonoBehaviour
{// 当前生命值public float currentBlood;// 最大生命值public float maxBlood;/// <summary>/// 处理角色受到伤害的方法/// </summary>/// <param name="damage">受到的伤害值</param>public void TakeDamage(float damage){// 减少当前生命值currentBlood -= damage;// 确保生命值不会低于0if (currentBlood < 0) currentBlood = 0;}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.TextCore.Text;/// <summary>
/// 处理攻击行为和伤害判定的组件
/// </summary>
public class Attack : MonoBehaviour
{// 攻击伤害值public float attackValue;// 是否处于无敌状态private bool isInvincible;// 上次造成伤害的时间private float lastTime;// 无敌状态持续时间(单位:秒)private float invincibleDuration = 2f;private void Awake(){// 初始化上次攻击时间lastTime = Time.time;}// Update在每一帧被调用void Update(){// 检查是否需要解除无敌状态if (isInvincible && Time.time - lastTime >= invincibleDuration){isInvincible = false;}}/// <summary>/// 当其他碰撞体持续停留在触发器内时调用/// </summary>/// <param name="collision">与之碰撞的碰撞体</param>private void OnTriggerStay2D(Collider2D collision){// 如果碰撞对象不是玩家,直接返回if (!collision.CompareTag("Player")) return;// 只有在非无敌状态下才造成伤害if (!isInvincible){// 获取被攻击对象的Character组件Character character = collision.GetComponent<Character>();// 对目标造成伤害character.TakeDamage(attackValue);// 进入无敌状态isInvincible = true;// 记录当前时间点lastTime = Time.time;}}
}

受伤动画

2.8 死亡

2.9 攻击

动画

判定

3.敌人

http://www.rkmt.cn/news/97403.html

相关文章:

  • 爆炸事件检测与识别 _ 基于YOLOv5-AFPN-P2345模型的改进实现
  • Refine+Next.js+Ant Design实战避坑指南:从版本冲突到性能优化的完整解决方案
  • CosyVoice ONNX模型部署终极指南:5大实战技巧快速掌握
  • 实用指南:HarmonyOS RelativeContainer相对布局:超越线性思维的约束艺术
  • Unity教学 项目1 2D赛车小游戏
  • KAREL编程实战手册:FANUC机器人数据交互核心技术解析
  • React Native Share:移动端跨平台分享解决方案
  • 严正声明
  • 从零构建企业专属Android应用商店:私有化部署完整方案
  • 华为开源盘古Pro MoE:720亿参数大模型如何重构AI效率边界
  • 再谈需求无止境,EAST和金融机构--SMP(软件制作平台)
  • iOS UI框架革命:DCFrame如何用数据驱动让开发效率提升300%
  • Zephyr编译优化终极指南:5个技巧提升嵌入式系统性能
  • TWiLight Menu++ 新手完全指南:从零开始掌握复古游戏启动器
  • 阿里巴巴千问APP上线:开源大模型在消费级AI中的应用实践 - 教程
  • 2025年靠谱的辊筒输送机/提升机输送机最新TOP厂家排名 - 品牌宣传支持者
  • debug.js实战指南:从安装到高级用法的完整教程
  • 70、Ubuntu 和 Linux 网络资源全解析
  • 2025年比较好的定制家具五金/品牌家具五金厂家推荐及采购指南 - 品牌宣传支持者
  • 2025年口碑好的浙江立体停车库链条/浙江摩托车链条高评价厂家推荐榜 - 品牌宣传支持者
  • Vue 3 + Vite
  • 2025年评价高的冷弯半圆管/后壁半圆管厂家最新推荐排行榜 - 行业平台推荐
  • CubeFS贡献者成长路线:从入门到专家的实战指南
  • 2025年比较好的手办亚克力展示架厂家最新TOP实力排行 - 品牌宣传支持者
  • 2025年质量好的自动化零件机械加工/五轴机械加工厂家最新推荐排行榜 - 行业平台推荐
  • 小区物业|基于springboot + vue小区居民物业管理系统(源码+数据库+文档)
  • 2025年拎拎壶学生保温杯行业内口碑厂家排行榜 - 行业平台推荐
  • 畅游游戏销售|基于springboot + vue畅游游戏销售管理系统(源码+数据库+文档)
  • 《学术迷宫的“智能指南针”:书匠策AI解锁毕业论文全周期新范式》
  • 深度解析Nacos命名空间异常:实战修复与防护指南