【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里解决



相关文章
|
25天前
【ShaderToy中图形效果转译到UnityShaderlab案例分享,实现圆形图像变异_Animation】
【ShaderToy中图形效果转译到UnityShaderlab案例分享,实现圆形图像变异_Animation】
|
25天前
|
图形学
【ShaderToy中图形效果转译到UnityShaderlab案例分享,实现七彩动画光圈_ColorfulCircle】
【ShaderToy中图形效果转译到UnityShaderlab案例分享,实现七彩动画光圈_ColorfulCircle】
|
11月前
|
机器人 图形学 Ruby
【Ruby 2D】【unity learn】控制敌人随机运动以及动画控制
【Ruby 2D】【unity learn】控制敌人随机运动以及动画控制
|
存储 图形学
90.pygame游戏-玩个球(play the ball)最终版
90.pygame游戏-玩个球(play the ball)最终版
90.pygame游戏-玩个球(play the ball)最终版
|
图形学
Unity【DoTween】- 如何使Transform Tween动画序列可编辑
Unity【DoTween】- 如何使Transform Tween动画序列可编辑
412 0
Unity【DoTween】- 如何使Transform Tween动画序列可编辑
Game->打砖块(源码:详解)
Game->打砖块(源码:详解)
118 0
|
XML Android开发 数据格式
实战 | 使用揭露动画(Reveal Effect)做一个丝滑的Activity转场动画
提笔之际(附总体思路) 最近跟几个小伙伴在实践一个项目,考虑到界面效果,我们决定使用揭露动画作为Activity的转场动画。 这里主要是我负责这部分的实现。
1421 0
Silverlight & Blend动画设计系列六:动画技巧(Animation Techniques)之对象与路径转化、波感特效
原文:Silverlight & Blend动画设计系列六:动画技巧(Animation Techniques)之对象与路径转化、波感特效   当我们在进行Silverlight & Blend进行动画设计的过程中,可能需要设计出很多效果不一的图形图像出来作为动画的基本组成元素。
1007 0