赞
踩
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public int health;
public float blinkTime;
public int blinks;
private Renderer myRender;
void Start()
{
myRender = GetComponent<Renderer>();
}
// Update is called once per frame
void Update()
{
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Enemy : MonoBehaviour
{
public int health;
public int damage;
public float changeTime;
public GameObject bloodEffect;
public PlayerHealth playerHealth;
private SpriteRenderer sr;
private Color originColor;
public void Start()
{
playerHealth = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>();
sr = GetComponent<SpriteRenderer>();
originColor = sr.color;
}
public void Update()
{
if(health <= 0)
{
Destroy(gameObject);
}
}
public void TakeDamage(int damage)
{
health -= damage;
FlashColor(changeTime);
Instantiate(bloodEffect,
new Vector3(transform.position.x , transform.position.y + 0.5f, transform.position.z),
Quaternion.identity);
GameController.cameraShake.Shake();
}
void FlashColor(float time)
{
//分别对应着R,G,B,透明度
sr.color = new Color(255, 255, 0, 255);
Invoke("ResetColor", time);
}
void ResetColor()
{
sr.color = originColor;
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Player") &&
other.GetType().ToString() == "UnityEngine.CapsuleCollider2D")
{
if(playerHealth != null)
{
playerHealth.DamagePlayer(damage);
}
}
}
}
先设置好参数。
闪烁成功,当health变为0时人物也会消失。
void BlinkPlayer(int numBlinks,float seconds)
{
StartCoroutine(DoBlinks(numBlinks, seconds));
}
IEnumerator DoBlinks(int numBlinks, float seconds)
{
for (int i = 0; i < numBlinks * 2; i++)
{
myRender.enabled = !myRender.enabled;
yield return new WaitForSeconds(seconds);
}
myRender.enabled = true;
这段通过协成延迟调用DoBlinks(int numBlinks, float seconds),然后再for循环中,每次循环时取消组件的Sprite Renderer让它变透明,再隔一段时间恢复,看起来就像闪烁。
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Player") &&
other.GetType().ToString() == "UnityEngine.CapsuleCollider2D")
判断是否是玩家:标签是Player,并且发生碰撞的是组件CapsuleCollider2D,并且非空对象
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。