Skip to content

Commit

Permalink
Fix DOTween warnings (kill anim sequences when obj dies)
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Sep 7, 2024
1 parent fbac0a4 commit 9499d00
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 11 additions & 1 deletion Assets/Scripts/Characters/DamageText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class DamageText : MonoBehaviour
{
private TextMeshPro _text;

private Sequence _animSequence;

void Awake()
{
_text = transform.Find("Text").GetComponent<TextMeshPro>();
Expand All @@ -27,14 +29,22 @@ private void Init(int value)
this.SetDamage(value);

// make the text jump up
transform.DOLocalJump(
_animSequence = transform.DOLocalJump(
transform.localPosition,
1.0f, // jump power
1, // num jumps
0.5f // duration
);
}

private void OnDestroy()
{
if (_animSequence != null)
{
_animSequence.Kill();
}
}

// usage: _damageTextPrefab.Spawn(transform, position, 10, 2.0f);

public DamageText Spawn(Transform parent, Vector2 position, int value, float duration = 1.0f)
Expand Down
14 changes: 11 additions & 3 deletions Assets/Scripts/Characters/PlayerText.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
Expand All @@ -8,6 +6,8 @@ public class PlayerText : MonoBehaviour
{
private TextMeshPro _text;

private Sequence _animSequence;

void Awake()
{
_text = transform.Find("Text").GetComponent<TextMeshPro>();
Expand All @@ -24,14 +24,22 @@ private void Init(string text)
this.SetText(text);

// make the text jump up
transform.DOLocalJump(
_animSequence = transform.DOLocalJump(
transform.localPosition,
1.5f, // jump power
1, // num jumps
1.5f // duration
);
}

private void OnDestroy()
{
if (_animSequence != null)
{
_animSequence.Kill();
}
}

public PlayerText Spawn(Transform parent, Vector2 position, string text, float duration = 1.0f)
{
// instantiate the damage text prefab
Expand Down

0 comments on commit 9499d00

Please sign in to comment.