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

相关文章:

  • 基于C-MTEB/CMedQAv2-rerankingv的Qwen3-1.7b模型微调-demo
  • 深度学习与图像处理案例 │ 图像分类(智能垃圾分拣器)
  • 通达OA服务器无公网IP网络,如何通过内网穿透实现外网远程办公访问OA系统
  • 三十二、【Linux网站服务器】搭建httpd服务器演示虚拟主机配置、网页重定向功能
  • [25-cv-08377]Hublot手表商标带着14把“死神镰刀“来收割权!卖家速逃!
  • Dify 从入门到精通(第 4/100 篇):快速上手 Dify 云端:5 分钟创建第一个应用
  • Python爬虫04_Requests豆瓣电影爬取
  • 下拉加载问题
  • 电商项目_核心业务_分布式事务
  • 【AI论文】单一领域能否助力其他领域?一项基于数据的、通过强化学习实现多领域推理的研究
  • 少林寺用什么数据库?
  • web:html表单提交数据
  • 亚马逊广告进阶指南:如何合理调配预算
  • 网络的学习 2 Socket
  • 深入剖析 RocketMQ 分布式事务:原理、流程与实践
  • GitPython02-Git使用方式
  • 大模型对比评测:Qwen2.5 VS Gemini 2.0谁更能打?
  • 《C++二叉搜索树原理剖析:从原理到高效实现教学》
  • 基于 Amazon Bedrock 与 Anthropic Claude 3 智能文档处理方案:从扫描件提取到数据入库全流程实践
  • 智能Agent场景实战指南 Day 26:Agent评估与性能优化
  • Python正则表达式精准匹配独立单词技巧
  • 【Dolphinscheduler】docker搭建dolphinscheduler集群并与安全的CDH集成
  • python | numpy小记(八):理解 NumPy 中的 `np.meshgrid`
  • 嵌入式linux驱动开发:什么是Linux驱动?深度解析与实战入门
  • 如何通过IT-Tools与CPolar构建无缝开发通道?
  • OriGene:一种可自进化的虚拟疾病生物学家,实现治疗靶点发现自动化
  • 【ESP32设备通信】-LAN8720与ESP32集成
  • MOEA/DD与MOEA/D的区别
  • 2024 年 NOI 最后一题题解
  • 算法精讲:二分查找(二)—— 变形技巧