3-2. 野猪-撞墙判定和等候计时
1.1-2. 安装 Unity 引擎和代码编辑器2.1-3. 素材导入和整理3.1-4. 场景绘制和叠层设置4.1-5. 有规则和动态瓦片5.2-1. 设置人物及基本组件6.2-2. 创建及配置新输入系统7.2-3. 实现移动和翻转8.2-4. 实现人物跳跃9.2-5. 物理环境监测及绘制10.2-6. 创建人物基本动画11.2-7. 人物行走设定12.2-8. 创建人物跳跃动画13.2-9. 实现人物下蹲逻辑和动画14.2-10. 人物属性及伤害计算15.2-11. 受伤和死亡的逻辑和动画16.2-12. 三段攻击动画的实现17.2-13. 实装攻击判定18.3-1. 野猪 - 基本的移动逻辑和动画
19.3-2. 野猪-撞墙判定和等候计时
20.3-3. 野猪-受伤及死亡的逻辑和动画21.3-4. 有限状态机&抽象类多态22.3-5. 追击状态的转换23.3-6. 蜗牛-基本的移动逻辑和动画24.3-7. 蜗牛 - 特殊技能状态的实现25.3-8. 蜜蜂 - 基本的移动逻辑和动画26.3-9. 蜜蜂 - 追击和攻击的实现27.4-1. 创建人物状态栏28.4-2. 血量更新逻辑的实现29.5-1. 滑墙及蹬墙跳的实现30.5-2. 滑铲的逻辑和动画的实现31.6-1. 摄像机跟随及攻击抖动实现32.6-2. 音源设置和音效播放33.6-3. 水和荆棘的逻辑实现34.7-1. 人物可互动标识35.7-2. 场景互动的逻辑实现36.7-3. 场景管理和切换37.7-4. 场景加载后的执行逻辑38.7-5. 场景淡入淡出效果39.7-6. 主场景制作40.7-7. 实现新的冒险逻辑41.8-1.存储点及画面效果42.8-2. 数据结构及坐标保存加载43.8-3. 人物数值及场景的保存加载44.8-4. 制作游戏结束面板45.8-5. 序列化保存数据文件46.9-1. 实现移动设备屏幕操控47.9-2. 暂停面板及声音控制48.9-3. 打包生成游戏49.05. 生成房间之间连线50.06. 实现随机地图检测左边和右边是否有地面
老师的代码写的是有问题的,见我扩展的代码
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class PhysicsCheck : MonoBehaviour
{
private CapsuleCollider2D coll;
[Header("监测参数")]
public bool manual;
public Vector2 bottomOffset;
public Vector2 bottomLeftOffset;
public Vector2 bottomRightOffset;
public Vector2 leftOffset;
public Vector2 rightOffset;
public float checkRaduis;
public LayerMask groundLayer;
[Header("状态")]
public bool isGround;
public bool isLeftGround;
public bool isRightGround;
public bool touchLeftWall;
public bool touchRightWall;
private void Awake()
{
coll = GetComponent<CapsuleCollider2D>();
//if (!manual)
//{
// rightOffset = new Vector2(coll.offset.x + coll.bounds.size.x / 2, coll.bounds.size.y / 2);
// leftOffset = new Vector2(coll.offset.x - coll.bounds.size.x / 2, coll.bounds.size.y / 2);
//}
}
void Update()
{
Check();
}
public void Check()
{
if (!manual)
{
int faceDir = 0;
if (transform.localScale.x > 0)
{
faceDir = 1;
}
else if (transform.localScale.x < 0)
{
faceDir = -1;
}
bottomOffset = new Vector2(faceDir * coll.offset.x, coll.bounds.size.y / 2 - coll.offset.y);
bottomLeftOffset = new Vector2(faceDir * coll.offset.x - coll.bounds.size.x / 2, coll.bounds.size.y / 2 - coll.offset.y);
bottomRightOffset = new Vector2(faceDir * coll.offset.x + coll.bounds.size.x / 2, coll.bounds.size.y / 2 - coll.offset.y);
leftOffset = new Vector2(faceDir * coll.offset.x - coll.bounds.size.x / 2, coll.bounds.size.y / 2);
rightOffset = new Vector2(faceDir * coll.offset.x + coll.bounds.size.x / 2, coll.bounds.size.y / 2);
}
// 监测地面
isGround = Physics2D.OverlapCircle((Vector2)transform.position + bottomOffset, checkRaduis, groundLayer);
isLeftGround = Physics2D.OverlapCircle((Vector2)transform.position + bottomLeftOffset, checkRaduis, groundLayer);
isRightGround = Physics2D.OverlapCircle((Vector2)transform.position + bottomRightOffset, checkRaduis, groundLayer);
// 墙体判断
touchLeftWall = Physics2D.OverlapCircle((Vector2)transform.position + leftOffset, checkRaduis, groundLayer);
touchRightWall = Physics2D.OverlapCircle((Vector2)transform.position + rightOffset, checkRaduis, groundLayer);
}
private void OnDrawGizmosSelected()
{
// 绘制监测区
Gizmos.DrawWireSphere((Vector2)transform.position + bottomOffset, checkRaduis);
Gizmos.DrawWireSphere((Vector2)transform.position + bottomLeftOffset, checkRaduis);
Gizmos.DrawWireSphere((Vector2)transform.position + bottomRightOffset, checkRaduis);
Gizmos.DrawWireSphere((Vector2)transform.position + leftOffset, checkRaduis);
Gizmos.DrawWireSphere((Vector2)transform.position + rightOffset, checkRaduis);
}
}
这段代码在运行起来之后,就会出现左、右、左下、右下、下共五个检测盒,并且能判断左边、右边、左下、右下、下是否有地面
即使野猪转向了,检测盒的范围也是正确的
野猪撞墙后等待几秒再修改方向
项目相关代码
合集:
勇士传说
分类:
unity / 勇士传说
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?