昨晚研究了一晚Boss近战判定,也找了一些方法,但始终找不到合适的
今天终于让我找到了[泪目]
让我们先看演示
这个效果是我们的Boss挥刀时不造成伤害,当火焰冒出来时再对主角造成伤害。
这个我讲详细点吧
步骤:
首先,我们创建两个空对象
左伤害和右伤害,当主角在攻击范围内时进行攻击,
然后我们挂载伤害脚本和点碰撞体
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);//这里是击退效果,其实可以不加负号,我前面写反了这里图方便加了个负号 } } }
对主角造成伤害并击退
然后是动画控制
目前是三个,但是我还在做,后面还有其他的状态
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工具里解决
这里我点击Add
Property,添加事件,在播放动画时,在冒火的帧上将前面我们设置的空对象设置成true,也就是leftAttack.SetActive(true);
这样我们就能够在特定的帧上面对主角进行伤害了,Boss控制脚本我们就播放动画,伤害在Animation里解决