更改总则
使被攻击的人变白
更改
Entity.cs
protected virtual void Start()
{
fx = GetComponentInChildren<EntityFX>();//获得EntityFX组件
anim = GetComponentInChildren<Animator>();
rb = GetComponent<Rigidbody2D>();
}
public virtual void Damage()
{
fx.StartCoroutine("FlashFX");//通过StartCoroutine调用FlashFX函数
Debug.Log(gameObject.name + "was Damaged");
}
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; }
public EntityFX fx { get; private set; }
#endregion
protected virtual void Awake()
{
}
protected virtual void Start()
{
fx = GetComponentInChildren<EntityFX>();//获得EntityFX组件
anim = GetComponentInChildren<Animator>();
rb = GetComponent<Rigidbody2D>();
}
protected virtual void Update()
{
}
public virtual void Damage()
{
fx.StartCoroutine("FlashFX");//通过StartCoroutine调用FlashFX函数
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
}
新添加-> EntityFX.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EntityFX : MonoBehaviour
{
private SpriteRenderer sr;
[Header("Flash Fx")]
[SerializeField] private float flashDuration;
[SerializeField] private Material hitMat;
private Material originalMat;
private void Start()
{
sr = GetComponentInChildren<SpriteRenderer>();//获取子组件 SpriteRender,为之后的对Render的操作做铺垫
originalMat = sr.material;//获取sr中的material,材料。先保存起来以便以后换回去
}
private IEnumerator FlashFX()//用IEnumerator,目的在于将材料变为白色后经过一段时间能够变回去,时间由flashDuration控制
{
sr.material = hitMat;
yield return new WaitForSeconds(flashDuration);//WaitForSeconds()函数,应该是等待一段时间后变为0,与yield配合。
sr.material = originalMat;
}
}
注意
由于本人C#不是很好,关于IEnumerator和StartCoroutine暂时就不深入了,我就按个人感觉进行注释了,如有错误,请多指正