更新总则
添加进行攻击动画时检测被攻击对象的功能
本质与collision检测相同,所以本文不过多阐述
更改
Entity.cs
新添加变量
public Transform attackCheck;
public float attackCheckRadius;
假装受到攻击的代码
public virtual void Damage()
{
Debug.Log(gameObject.name + "was Damaged");
}
在画线的代码中添加了画圆的代码:
Gizmos.DrawWireSphere(attackCheck.position, attackCheckRadius);
protected virtual void OnDrawGizmos()
{
Gizmos.DrawLine(groundCheck.position, new Vector3(groundCheck.position.x, groundCheck.position.y - groundCheckDistance));
Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x - wallCheckDistance, wallCheck.position.y));
Gizmos.DrawWireSphere(attackCheck.position, attackCheckRadius);
}
其表示效果如下
PlayerAnimationTriggers.cs
新添代码
private void AttackTrigger()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(player.attackCheck.position, player.attackCheckRadius);
foreach(var hit in colliders)
{
if(hit.GetComponent<Enemy>()!=null)
{
hit.GetComponent<Enemy>().Damage();
}
}
}
Enemy_SkeletonAnimationTriggers.cs
private void AttackTrigger()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(enemy.attackCheck.position, enemy.attackCheckRadius);
foreach (var hit in colliders)
{
if (hit.GetComponent<Player>() != null)
{
hit.GetComponent<Player>().Damage();
}
}
}
更改总代码
Entity.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Entity : MonoBehaviour
{
[Header("Collision info")]
public Transform attackCheck;
public float attackCheckRadius;
[SerializeField] protected Transform groundCheck;
[SerializeField] protected float groundCheckDistance;
[SerializeField] protected Transform wallCheck;
[SerializeField] protected float wallCheckDistance;
[SerializeField] protected LayerMask whatIsGround;
//flip Info
public int facingDir { get; private set; } = 1;
protected bool facingRight = true;
#region Components
public Rigidbody2D rb { get; private set; }
public Animator anim { get; private set; }
#endregion
protected virtual void Awake()
{
}
protected virtual void Start()
{
anim = GetComponentInChildren<Animator>();
rb = GetComponent<Rigidbody2D>();
}
protected virtual void Update()
{
}
public virtual void Damage()
{
Debug.Log(gameObject.name + "was Damaged");
}
#region Velocity
public void SetZeroVelocity()
{
rb.velocity = new Vector2(0, 0);
}
public void SetVelocity(float _xVelocity, float _yVelocity)
{
rb.velocity = new Vector2(_xVelocity, _yVelocity);
FlipController(_xVelocity);
}
#endregion
#region Collision
public virtual bool IsGroundDetected() => Physics2D.Raycast(groundCheck.position, Vector2.down, groundCheckDistance, whatIsGround);
public virtual bool IsWallDetected() => Physics2D.Raycast(wallCheck.position, Vector2.right * facingDir, wallCheckDistance, whatIsGround);
protected virtual void OnDrawGizmos()
{
Gizmos.DrawLine(groundCheck.position, new Vector3(groundCheck.position.x, groundCheck.position.y - groundCheckDistance));
Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x - wallCheckDistance, wallCheck.position.y));
Gizmos.DrawWireSphere(attackCheck.position, attackCheckRadius);
}
#endregion
#region Flip
public virtual void Flip()
{
facingDir = facingDir * -1;
facingRight = !facingRight;
transform.Rotate(0, 180, 0);
}
public virtual void FlipController(float _x)
{
if (_x > 0 && !facingRight)
Flip();
else if (_x < 0 && facingRight)
Flip();
}
#endregion
}
PlayerAnimationTriggers.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAnimationTriggers : MonoBehaviour
{
private Player player => GetComponentInParent<Player>();
private void AnimationTrigger()
{
player.AnimationTrigger();
}
private void AttackTrigger()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(player.attackCheck.position, player.attackCheckRadius);
foreach(var hit in colliders)
{
if(hit.GetComponent<Enemy>()!=null)
{
hit.GetComponent<Enemy>().Damage();
}
}
}
}
Enemy_SkeletonAnimationTriggers.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_SkeletonAnimationTriggers : MonoBehaviour
{
private Enemy_Skeleton enemy => GetComponentInParent<Enemy_Skeleton>();
private void AnimationTrigger()
{
enemy.AnimationFinishTrigger();
}
private void AttackTrigger()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(enemy.attackCheck.position, enemy.attackCheckRadius);
foreach (var hit in colliders)
{
if (hit.GetComponent<Player>() != null)
{
hit.GetComponent<Player>().Damage();
}
}
}
}