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

学习游戏制作记录(冻结敌人时间与黑洞技能)7.30

1.实现剑击中敌人时冻结敌人时间

Enemy脚本:


public float defaultMoveSpeed;//默认速度

defaultMoveSpeed = moveSpeed;//Awake()中设置

    public virtual void FreezeTime(bool _timeFreeze)//冻结设置函数
{
if (_timeFreeze)
{
moveSpeed = 0;
anim.speed = 0;
}

        else
{
moveSpeed = defaultMoveSpeed;
anim.speed = 1;
}
}

    protected virtual IEnumerator FreezeTimeFor(float _seconds)//冻结协程
{
FreezeTime(true);

        yield return new WaitForSeconds(_seconds);

        FreezeTime(false);//解除冻结

    }

Sword_Skill脚本:

    [Header("FreezeTime info")]
[SerializeField] private float freezeTimeDuration;//冻结时间

newSkillScript.SetupSword(finalDir, swordGravity, player,freezeTimeDuration);//设置

Sword_Skill_Control脚本:

    [Header("FreezeTime info")]
private float freezeTimeDuration;//接受传入的冻结时间

 if(collision.GetComponent<Enemy>() != null)//触发器函数中调用
{
Enemy enemy = collision.GetComponent<Enemy>();
enemy.Damage();//将这两句提取为SwordSkillDamage方法,方便调用
enemy.StartCoroutine("FreezeTimeFor", freezeTimeDuration);//启用协程

 }

SwordSkillDamage(enemyTarget[enemyIndex].GetComponent<Enemy>());//弹跳逻辑函数中

 SwordSkillDamage(hit.GetComponent<Enemy>());//旋转逻辑函数中

2.实现剑超过一定时间会自行销毁

Sword_Skill_Control脚本:

    public void DestroyMe()//自毁函数
{
Destroy(gameObject);
}

Invoke("DestroyMe", 7f);//7秒后自毁,SetupSword调用

3.实现在技能管理器中设置返回速度和弹跳速度(其它类似)

Sword_Skill_Control脚本:


returnSpeed = _returnSpeed;

bouncingSpeed= _BounceSpeed;//在各自的设置函数里添加

Sword_Skill脚本:
[SerializeField] private float bounceSpeed;

[SerializeField] private float returnSpeed;//可以直接修改

 newSkillScript.SetupSword(finalDir, swordGravity, player,freezeTimeDuration,returnSpeed);

newSkillScript.SetupBounce(true, bounceAmount,bounceSpeed);//传入参数

4.实现黑洞与快速时间事件

当释放黑洞技能时,会创建一个随时间增大的圆形物体,它会检测圆内的所有敌人并冻结敌人时间,并且在每个敌人头上会生成一个快捷预制按键,当玩家按下按键时会调用函数将相应的敌人添加到一个未来的目标列表中,当所有按键都被按下或者时间耗尽时,我们会在目标列表敌人处创建一个克隆攻击。

实现黑洞的增大与敌人列表获取

准备好Blackhole_Skill_Control脚本和圆形对象(添加碰撞器并勾选触发器):

    public float maxSize;//最大尺寸
public float growSpeed;//变化速度
public bool canGrow;//是否可以变大

    public List<Transform> EnemyTarget;//获取的敌人
private void Update()
{
if (canGrow)
{
transform.localScale=Vector2.Lerp(transform.localScale,new Vector2(maxSize, maxSize),growSpeed*Time.deltaTime);//Lerp是缓慢变化,变化transform的规模
}

       
}

    private void OnTriggerEnter2D(Collider2D collision)//触发器函数
{
if(collision.GetComponent<Enemy>() != null)
{
EnemyTarget.Add(collision.GetComponent<Enemy>().transform);
}
}

实现黑洞中敌人的冻结和敌人头顶预制快捷键的生成

Blackhole_Skill_Control脚本:

        if(collision.GetComponent<Enemy>() != null)
{
collision.GetComponent<Enemy>().FreezeTime(true);//直接调用冻结函数
}

安装TextMeshPro

创建一个黑色方块对象,添加ui文字

设置黑色背景

画布设置,调整大小

文本设置,记得居中

最终效果:

创建Blackhole_HotKey_Control脚本挂载在上面的对象:

    private KeyCode myhotKey;//我的预制键
private TextMeshProUGUI myText;//文本

    public void SetupHotKey(KeyCode _mynewhotKey)//触发器里会调用它
{
myText = GetComponentInChildren<TextMeshProUGUI>();

        myhotKey = _mynewhotKey;
myText.text =_mynewhotKey.ToString();//修改文本
}

    public void Update()
{
if(Input.GetKeyUp(myhotKey)) //调试
{
Debug.Log("i enter"+ myhotKey.ToString());

        }
}

Blackhole_Skill_Control脚本:

    [SerializeField] private GameObject hotKeyPrefab;//按键预制体
[SerializeField] private List<KeyCode> KeyCodeList;//键的列表,自行添加

    private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.GetComponent<Enemy>() != null)
{
collision.GetComponent<Enemy>().FreezeTime(true);//可以把下面的代码提取方法为CreateHotKey()

       if(KeyCodeList.Count<=0)
{
return;
}

            GameObject newhotKey= Instantiate(hotKeyPrefab,collision.transform.position+new Vector3(0,2),Quaternion.identity);//在敌人头上创建预制键

            KeyCode chooseKey = KeyCodeList[Random.Range(0,KeyCodeList.Count)];//随机选择一个键码


            KeyCodeList.Remove(chooseKey);//选择后移除防止重复

            Blackhole_HotKey_Control newhotKeyScript=newhotKey.GetComponent<Blackhole_HotKey_Control>();

            newhotKeyScript.SetupHotKey(chooseKey);//设置键码
}
}

}

演示:

实现按下相应按键将敌人添加到列表中并且使按键不可见:

Blackhole_Skill_Control脚本:

 private List<Transform> EnemyTarget=new List<Transform>();

 newhotKeyScript.SetupHotKey(chooseKey, collision.transform, this);//传入参数

public void AddEnemyToList(Transform _enemyTransform) => EnemyTarget.Add(_enemyTransform);//添加列表函数

Blackhole_HotKey_Control脚本:

private SpriteRenderer sr;

private Transform myEnemy;//每个按键对应的敌人
private Blackhole_Skill_Control Blackhole;//按键对应的黑洞

    public void SetupHotKey(KeyCode _mynewhotKey,Transform _myEnemy,Blackhole_Skill_Control _Blackhole)//重新初始化
{
myText = GetComponentInChildren<TextMeshProUGUI>();

        myhotKey = _mynewhotKey;
myText.text =_mynewhotKey.ToString();

        myEnemy = _myEnemy;
Blackhole = _Blackhole;
}

    public void Update()
{
if(Input.GetKeyUp(myhotKey)) 
{
Blackhole.AddEnemyToList(myEnemy);//添加到列表

            myText.color = Color.clear;//设置透明
sr.color = Color.clear;
}
}

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

相关文章:

  • 【音视频】WebRTC 开发环境搭建-Web端
  • Apple基础(Xcode②-Flutter结构解析)
  • ica1靶机练习
  • K8s 备份与恢复利器:Velero 实战指南
  • MySQL常见面试题
  • springboot本地访问https链接,证书错误
  • Spark的宽窄依赖
  • Kubernetes 中 ConfigMap 与 Secret 的深度解析
  • gaussdb demo示例
  • Spring Cloud Gateway静态路由实战:Maven多模块高效配置指南
  • 时序数据库厂商 TDengine 发布 AI 原生的工业数据管理平台 IDMP,“无问智推”改变数据消费范式
  • ES 文件浏览器:多功能文件管理与传输利器
  • 数据建模怎么落地?从概念、逻辑到物理模型,一文讲请!
  • Kubernetes高级调度02
  • 《超级秘密文件夹》密码遗忘?试用版/正式版找回教程(附界面操作步骤)
  • AI任务相关解决方案11-基于 Qwen3+langchain+Agent 的学术论文编辑平台系统搭建与开发案例
  • Redis学习------缓存穿透
  • 【Python系列】如何安装无 GIL 的 Python 3.13
  • 区块链、Web3、元宇宙与AI融合的安全挑战:2025年深度分析
  • ICODE SLIX2有密钥保护的物流跟踪、图书馆管理ISO15693标签读写Delphi源码
  • 第七章:进入Redis的SET核心
  • 论文阅读:《多目标和多目标优化的回顾与评估:方法和算法》
  • 算法思想之 BFS 解决 最短路问题
  • Zookeeper符合cap中的AP还是CP
  • 【科研绘图系列】R语言绘制绝对量柱状堆积图+环形图数量统计+特数量标注
  • Python并发与性能革命:自由线程、JIT编译器的深度解析与未来展望
  • 【JVM篇11】:分代回收与GC回收范围的分类详解
  • ADA4622-2ARMZ-R7 ADI双通道精密运算放大器 ±0.25μV超低失调+0.1μV/°C温漂
  • OpenBayes 教程上新丨仅激活 3B 参数可媲美 GPT-4o,Qwen3 深夜更新,一手实测来了!
  • Vue3 Composition API