当前位置: 首页 > 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.lryc.cn/news/604498.html

相关文章:

  • nginx安装配置Lua模块的支持
  • 我的世界模组开发教程——资源(1)
  • 【AI】入门级提示词模板:适用于ChatGPT、文心一言等主流模型
  • 【ee类保研面试】数学类---线性代数
  • 从0开始学习R语言--Day62--RE插补
  • JVM对象创建与内存分配机制深度剖析
  • 基于Catboost的铁路交通数据分析及列车延误预测系统的设计与实现【全国城市可选、欠采样技术】
  • 【JVM篇11】:分代回收与GC回收范围的分类详解
  • 数据分析师进阶——95页零售相关数据分析【附全文阅读】
  • JVM 性能调优实战:让系统性能 “飞” 起来的核心策略
  • 观远 ChatBI 完成 DeepSeek-R1 大模型适配:开启智能数据分析跃升新篇
  • 【Spring】一文了解SpringMVC的核心功能及工作流程,以及核心组件及注解
  • Linux 日志管理与时钟同步详解
  • GIS工程师面试题
  • GitHub 热门项目 PandaWiki:零门槛搭建智能漏洞库,支持 10 + 大模型接入
  • UG NX二次开发(Python)-根据封闭曲线创建拉伸特征
  • Class27GoogLeNet
  • 实用性方案:高效处理图片拼接的正确打开方式
  • sed编程入门
  • [Agent开发平台] Coze Loop开源 | 前端 | typescript架构API速查
  • Python Pandas.get_dummies函数解析与实战教程
  • 【iOS】weak修饰符
  • 磁盘io查看命令iostat与网络连接查看命令netstat
  • [Qt]QString 与Sqlite3 字符串互动[汉字不乱码]
  • iOS电池寿命与App能耗监测实战 构建完整性能监控系统
  • 常见CMS获取webshell的方法-靶场练习
  • 2025年自动化工程与计算机网络国际会议(ICAECN 2025)
  • C++菱形虚拟继承:解开钻石继承的魔咒
  • 3D空间中的变换矩阵
  • 应用药品 GMP 证书识别技术,实现证书信息的自动化、精准化提取与核验