Play Animation (blink opacity) on hit

This commit is contained in:
2023-10-17 12:04:43 +02:00
parent cd37cd4522
commit 899427d70c
5 changed files with 45 additions and 1 deletions
+19
View File
@@ -1,3 +1,5 @@
using System.Collections;
using Unity.VisualScripting.FullSerializer;
using UnityEngine; using UnityEngine;
[ [
@@ -52,6 +54,7 @@ public class Ennemy : MonoBehaviour
if (collision.gameObject.CompareTag("Player")) if (collision.gameObject.CompareTag("Player"))
{ {
InvokeRepeating(nameof(AttackPlayer), 0, 1f); InvokeRepeating(nameof(AttackPlayer), 0, 1f);
_player.GetComponent<PlayerCollisions>().AnimateHit();
} }
} }
@@ -67,4 +70,20 @@ public class Ennemy : MonoBehaviour
{ {
_player.GetComponent<PlayerManager>().TakeDamage(Damages); _player.GetComponent<PlayerManager>().TakeDamage(Damages);
} }
public void AnimateHit()
{
StartCoroutine(IAnimateHit());
}
private IEnumerator IAnimateHit()
{
for(int i = 0; i < 6; i++)
{
_sr.color = new Color(_sr.color.r, _sr.color.g, _sr.color.b, 0f);
yield return new WaitForSeconds(.1f);
_sr.color = new Color(_sr.color.r, _sr.color.g, _sr.color.b, 1f);
yield return new WaitForSeconds(.1f);
}
}
} }
+23 -1
View File
@@ -1,13 +1,19 @@
using System.Collections;
using UnityEngine; using UnityEngine;
[RequireComponent(typeof(PlayerManager))] [
RequireComponent(typeof(PlayerManager)),
RequireComponent(typeof(SpriteRenderer))
]
public class PlayerCollisions : MonoBehaviour public class PlayerCollisions : MonoBehaviour
{ {
private PlayerManager _pm; private PlayerManager _pm;
private SpriteRenderer _sr;
private void Start() private void Start()
{ {
_pm = GetComponent<PlayerManager>(); _pm = GetComponent<PlayerManager>();
_sr = GetComponent<SpriteRenderer>();
} }
private void OnTriggerStay2D(Collider2D collision) private void OnTriggerStay2D(Collider2D collision)
@@ -37,4 +43,20 @@ public class PlayerCollisions : MonoBehaviour
} }
} }
} }
public void AnimateHit()
{
StartCoroutine(IAnimateHit());
}
private IEnumerator IAnimateHit()
{
for(int i = 0; i < 6; i++)
{
_sr.color = new Color(_sr.color.r, _sr.color.g, _sr.color.b, 0f);
yield return new WaitForSeconds(.1f);
_sr.color = new Color(_sr.color.r, _sr.color.g, _sr.color.b, 1f);
yield return new WaitForSeconds(.1f);
}
}
} }
+1
View File
@@ -20,6 +20,7 @@ public class Arrow : MonoBehaviour
if (collision.CompareTag("Ennemy")) if (collision.CompareTag("Ennemy"))
{ {
collision.gameObject.GetComponent<Ennemy>().Life -= Damages; collision.gameObject.GetComponent<Ennemy>().Life -= Damages;
collision.gameObject.GetComponent<Ennemy>().AnimateHit();
Destroy(gameObject); Destroy(gameObject);
} }
+1
View File
@@ -28,6 +28,7 @@ public class Sword : MonoBehaviour
if (collision.CompareTag("Ennemy") && collision.gameObject != null) if (collision.CompareTag("Ennemy") && collision.gameObject != null)
{ {
collision.GetComponent<Ennemy>().Life -= Damages; collision.GetComponent<Ennemy>().Life -= Damages;
collision.gameObject.GetComponent<Ennemy>().AnimateHit();
} }
} }
} }
+1
View File
@@ -18,6 +18,7 @@ public class LaserBeam : MonoBehaviour
if (collision.CompareTag("Ennemy")) if (collision.CompareTag("Ennemy"))
{ {
collision.gameObject.GetComponent<Ennemy>().Life -= Damages; collision.gameObject.GetComponent<Ennemy>().Life -= Damages;
collision.gameObject.GetComponent<Ennemy>().AnimateHit();
} }
} }