Home > Back-end >  How to create paint on the floor every time my Unity character dies?
How to create paint on the floor every time my Unity character dies?

Time:12-27

enter image description here

red area in the image. The thing about my character dying in combat. Here's how to live paint on the floor when every character dies. enter image description here

enter image description here

i tried this but unfortunately it didn't work

CodePudding user response:

The first part of code contains void RunTowardsTarget(). Do you call this void one time or every frame? It looks like you call it one time but there is if statement in this method so the condition will compare one time. I think you need to use coroutine.

public IEnumerator RunTowardsTarget() 
{
    while(Vector3.Distance(transform.position, targetRunner.position) > 1)
    {
        targetRunner.position = targetRunner.position   speed;
        yield return null;
    }

    //your destroy calls
    onRunnerDied?.Invoke();
        
    Destroy(targetRunner.gameObject);
    Destroy(gameObject);
}

CodePudding user response:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Enemy : MonoBehaviour
{
 
    
    enum State {Idle , Running}

    [Header(" Settings ")]
    [SerializeField] private float searchRadius;
    [SerializeField] private float moveSpeed;
    private State state;
    private Transform targetRunner;
    public GameObject splashEffect;


    [Header(" Events ")]
    public static System.Action onRunnerDied;


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        ManageState();
    }

    private void ManageState()
    {
        switch (state)
        {
            case State.Idle:
                SearchForTarget();
            break;

            case State.Running:
                RunTowardsTarget();
            break;
        }
            

    }

    private void SearchForTarget()
    {
        Collider[] detectedColliders = Physics.OverlapSphere(transform.position, searchRadius);

        for(int i = 0; i < detectedColliders.Length; i  )
        {
            if (detectedColliders[i].TryGetComponent(out Runner runner))
            {
                if (runner.IsTarget())
                {
                    continue;
                }

                runner.SetTarget();
                targetRunner = runner.transform;

                StartRunningTowardsTarget();
            }
        }
    }

    private void StartRunningTowardsTarget()
    {
        state = State.Running;

        GetComponent<Animator>().Play("Run");
    }

    private void RunTowardsTarget()
    {
        if (targetRunner == null)
        {

  

            return;

        }

        transform.position = Vector3.MoveTowards(transform.position, targetRunner.position, Time.deltaTime * moveSpeed);     

        if(Vector3.Distance(transform.position,targetRunner.position) < .1f)
        {
            onRunnerDied?.Invoke();
            
            Destroy(targetRunner.gameObject);

            
            Destroy(gameObject);



            GameObject splash = Instantiate(splashEffect);
            splash.transform.SetParent(targetRunner.transform);
            splash.transform.localEulerAngles = new Vector3(90, Random.RandomRange(0, 360), 0);
            float randomScale = Random.RandomRange(0.18f, 0.25f);
            splash.transform.localScale = new Vector3(randomScale, randomScale, 1);
            splash.transform.position = new Vector3(transform.position.x, transform.position.y - 0.22f, transform.position.z);




        }



    }






}
  • Related