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

学习游戏制作记录(改进剑投掷状态)7.28

 1.实现玩家瞄准状态的实时翻转

PlayerAimSwordState脚本:

    Vector2 mouseposition=Camera.main.ScreenToWorldPoint(Input.mousePosition);//鼠标位置

    if (_Player.transform.position.x < mouseposition.x && _Player.facingDir == -1)//位置检测
_Player.Flip();

    else if (_Player.transform.position.x > mouseposition.x && _Player.facingDir == 1)
_Player.Flip();
}//Update()中调用

2.实现进入接剑状态,并实现接剑的翻转和接住剑的冲击力

Player脚本:

public float SwordReturnImpact;//击退力

    public void CatchTheSword()//之前叫clearTheSword,改一下名字
{
stateMachine.ChangeState(CatchSword);//进入接剑状态
Destroy(sword);
}

在接剑动画的最后一帧上添加帧事件调用函数AnimationTrigger()

它会见TriggerCalled设置为True

PlayerCatchSwordState脚本:

public GameObject sword;//剑组件

    public override void Enter()
{
base.Enter();

        sword = _Player.sword;

        if (_Player.transform.position.x < sword.transform.position.x && _Player.facingDir == -1)//翻转
_Player.Flip();

        else if (_Player.transform.position.x > sword.transform.position.x && _Player.facingDir == 1)
_Player.Flip();

        rb.velocity = new Vector2(_Player.SwordReturnImpact * -_Player.facingDir, rb.velocity.y);//给玩家一个后退力
}

    public override void Update()
{
base.Update();

        if(TriggerCalled)//动画结束后进入待机状态
{
_PlayerStateMachine.ChangeState(_Player.idleState);
}
}

3.修复瞄准同时按下移动键的滑动和接剑时按移动会抵消冲击力的bug

在PlayerAimSwordState脚本和PlayerCatchSwordState脚本的Exit()中加上:


      _Player.StartCoroutine("BusyFor",.2f);//是玩家退出状态时进入0.2秒的忙碌状态,这状态下不能行走,关于这个协程,在我之前的文章里有出现

4.修复剑在即将碰到敌人或地面时按下返回键会行下掉的bug

Sword_Skill_Control脚本:

    public void ReturnSword()
{
rb.constraints = RigidbodyConstraints2D.FreezeAll;//锁定xyz即可
// rb.isKinematic=false;保持它为非运动学
transform.parent = null;

        isReturning = true;
}

剑在敌人后返回后会击中敌人,但我们目前还未添加剑的伤害函数

在OnTriggerEnter2D中添加

        if (isReturning)
{
return;
}

这样就直接返回,无任何影响

5.修复玩家奔跑时进入瞄准会滑动的bug

PlayerAimSwordState脚本:

_Player.SetZeroVelocity();//Update中调用设值速度为0

今天的内容比较少,希望大家可以把脚本代码进行一些整理。

http://www.dtcms.com/a/302665.html

相关文章:

  • B3927 [GESP202312 四级] 小杨的字典
  • Unity_UI_NGUI_锚点组件
  • Ansible安装与入门
  • AP-0316:集 USB 即插即用、智能降噪于一体的多功能 AI 声卡,重新定义清晰语音交互
  • Adobe Experience Manager (AEM) Sites|企业级内容管理与数字体验平台
  • 4.Linux 应用程序的安装和管理
  • Win 11:系统还原 C 盘最早的还原点,以整治 glu32.dll
  • 哈夫曼树的解析
  • 从技术架构到应用落地:无忧秘书 AI 的智能交互逻辑拆解
  • C/C++ 调用lua脚本,lua脚本调用另一个lua脚本
  • Rastrigin函数简介
  • Revo Uninstaller Pro专业版领取:2025最佳Windows软件卸载工具
  • org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path
  • 数据结构之顺序表链表栈
  • 设计模式(十六)行为型:解释器模式详解
  • LeetCode 2044题:统计按位或能得到最大值的子集数目(原创)
  • RabbitMQ概念与管理端配置说明
  • Ubuntu “apt”安装
  • 【Apache Tomcat】
  • Thymeleaf语法大全:30个实用示例
  • UE5 UI自适应 DPI缩放
  • 分布式微服务--核心组件与架构关系(一)
  • 前端面试题--副本
  • 【n8n教程笔记——工作流Workflow】文本课程(第一阶段)——5.7 调度工作流 (Scheduling the workflow)
  • <PLC><西门子><modbusTCP>在西门子S7-1200系列PLC中,如何设置modbusTCP通讯?
  • 深度学习核心模型架构解析:Transformer自注意力机制与Query-Key-Value投影的向量空间几何解释
  • 【GitHub Workflows 基础(一)】认识 .github/workflows/ 下的多个工作流
  • ubuntu qt环境下出现No suitable kits found解决方案
  • 国产化PDF处理控件Spire.PDF教程:Java 提取 PDF 图片,高质量提取与图片过滤技巧
  • ros2的package.xml和rosdep