【sunny land】利用Animation编辑器实现近战敌人判定

简介: 【sunny land】利用Animation编辑器实现近战敌人判定

昨晚研究了一晚Boss近战判定,也找了一些方法,但始终找不到合适的

今天终于让我找到了[泪目]

让我们先看演示62ada18cb9e844f0b96ceae1e71d312f.gif

这个效果是我们的Boss挥刀时不造成伤害,当火焰冒出来时再对主角造成伤害。

这个我讲详细点吧

步骤:

首先,我们创建两个空对象

8908982a5c664317bd76a884318afb9f.png

左伤害和右伤害,当主角在攻击范围内时进行攻击,

然后我们挂载伤害脚本和点碰撞体

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeleeAttack : MonoBehaviour
{
    // Start is called before the first frame update
    public float hurt;
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        foxcontroller fox = other.gameObject.GetComponent<foxcontroller>();
        Rigidbody2D rb = other.gameObject.GetComponent<Rigidbody2D>();
        if (fox != null){
            Vector2 Force = new Vector2(transform.position.x - rb.position.x, 0);
            fox.ChangeHealth(-2);//近战攻击扣两滴血,要是难度太高再削
            rb.AddForce(-Force * hurt);//这里是击退效果,其实可以不加负号,我前面写反了这里图方便加了个负号
        }
    }
}


对主角造成伤害并击退

然后是动画控制



68610907373043de9a6270f1733753eb.png

目前是三个,但是我还在做,后面还有其他的状态

    public GameObject LeftAttack;
    public GameObject RightAttack;
    public float MeleeAttackRadial;
    private bool Attacking;
    public  bool canAttack = true;
    public float canAttackingTime = 4f;
    public float Timerr;
    void Start()
    {
        Timer = changeTime;
        animator = GetComponent<Animator>();
        Timerr = canAttackingTime;
    }
    // Update is called once per frame
    void Update()
    {
        Attack();
        runNormal();
        if (Timerr > 0)
        {
            Timerr -= Time.deltaTime;
            canAttack = false;
        }
        else canAttack = true;
    }
private void Attack()
    {
        if (fox.transform.position.x - transform.position.x > 9 || fox.transform.position.x - transform.position.x < -9)
        {
            LeftAttack.SetActive(false);
            RightAttack.SetActive(false);
        }
        if (!canAttack)
        {
            Attacking = false;
            animator.SetBool("Attacking", false);
            return;
        }
        if (fox.transform.position.x - transform.position.x < 9 && fox.transform.position.x - transform.position.x > 0&&direction == 1)
        {
            Timerr = canAttackingTime;
            Attacking = true;
            //RightAttack.SetActive(true);
            animator.SetFloat("look", fox.transform.position.x - transform.position.x);
            animator.SetBool("Attacking", true);
        }
        else if (fox.transform.position.x - transform.position.x > -9 && fox.transform.position.x - transform.position.x < 0&&direction == -1)
        {
            Timerr = canAttackingTime;
            Attacking = true;
            //LeftAttack.SetActive(true);
            animator.SetFloat("look", fox.transform.position.x - transform.position.x);
            animator.SetBool("Attacking", true);
        }
        else
        {
            Attacking = false;
            LeftAttack.SetActive(false);
            RightAttack.SetActive(false);
            animator.SetBool("Attacking", false);
        }
    }

攻击代码,当Boss攻击冷却好并且主角处于攻击范围内并面朝主角时,开始攻击主角,脚本里我只播放攻击动画,判定我在Animation工具里解决



83ce0ebf57214222ac7526977be56ec4.png


这里我点击Add Property,添加事件,在播放动画时,在冒火的帧上将前面我们设置的空对象设置成true,也就是leftAttack.SetActive(true);


这样我们就能够在特定的帧上面对主角进行伤害了,Boss控制脚本我们就播放动画,伤害在Animation里解决



相关文章
|
开发工具
Pyside6-第十二篇-QSlider滑动条
Pyside6-第十二篇-QSlider滑动条
392 0
|
iOS开发
iOS开发 - 滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和Masonry简单使用)
iOS开发 - 滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和Masonry简单使用)
203 0
iOS开发 - 滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和Masonry简单使用)
|
XML 图形学 数据格式
Animation组合动画踩坑-实现循环播放动画,可控制次数
Animation组合动画踩坑-实现循环播放动画,可控制次数
|
存储 图形学
90.pygame游戏-玩个球(play the ball)最终版
90.pygame游戏-玩个球(play the ball)最终版
143 0
90.pygame游戏-玩个球(play the ball)最终版
|
图形学
Unity【DoTween】- 如何使Transform Tween动画序列可编辑
Unity【DoTween】- 如何使Transform Tween动画序列可编辑
484 0
Unity【DoTween】- 如何使Transform Tween动画序列可编辑
|
前端开发 容器
Silverlight & Blend动画设计系列十二:三角函数(Trigonometry)动画之自由旋转(Free-form rotation)
原文:Silverlight & Blend动画设计系列十二:三角函数(Trigonometry)动画之自由旋转(Free-form rotation)   说到对象的旋转,或许就会联想到对象角度的概念。
979 0
|
前端开发 数据格式 XML
Expression Design与Blend制作滚动的小球动画教程
原文:Expression Design与Blend制作滚动的小球动画教程 一,开发工具 Microsoft Expression Design & Blend 4.0 (3.0亦可)。 这两款软件可以在微软Expression官方网站上下载:http://expression.
1089 0