1.为Player添加一个子对象并为子对象添加Polygon Collider 2D,在Polygon Collider 2D中选中Is Trigger,根据攻击动画调整Collider大小。
2.添加PlayerAttack脚本,拖到添加的子对象上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
public int damage;
public float time;
public float startTime;
private Animator anim;
private PolygonCollider2D collider2D;
// Start is called before the first frame update
void Start()
{
anim = GameObject.FindGameObjectWithTag(“Player”).GetComponent();
collider2D = GetComponent();
}
// Update is called once per frame
void Update()
{
Attack();
}
void Attack()
{
if (Input.GetButtonDown("Attack"))
{
collider2D.enabled = true;
anim.SetTrigger("Attack");
StartCoroutine(DisableHitBox());
}
}
IEnumerator StartAttack()
{
yield return new WaitForSeconds(startTime);
collider2D.enabled = true;
StartCoroutine(DisableHitBox());
}
IEnumerator DisableHitBox()
{
yield return new WaitForSeconds(time);
collider2D.enabled = false;
}
}`
3.在inspector页面,根据动画调整开始和结束时间,以及伤害。