Unity: My enemy projectile is being destroyed before ever leaving it's spawn location. What am i doing wrong?

105 views Asked by At

Like the title says, the enemy projectiles are not launching. They are spawned and destroyed in the same place. They do not fire toward the target. Code in link:

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

public class MonkeyController : MonoBehaviour {

    private const int MAX_INJURES = 1;

    public float DurationMovement = 2f;
    public Transform SpawnLocation;
    public Transform[] PatrolPoints;
    public GameObject MonkeyProjectile;
    public Rigidbody2D Abby;
    public float AttackDelay = 0.75f;
    public float ProjectileSpeed = 1;
    public int ProjectilesCount = 4;

    public float activePosition;
    public float passivePosition;

    private List<GameObject> ProjectileList = new List<GameObject>();
    private int PatrolIndex = 0;


    private int injures = 0;

    private bool canPatrol;

    private Coroutine fireCoroutine;
    private Coroutine patrolCoroutine;
    private Coroutine movementCoroutine;

    WaitForSeconds delay;
    Vector2 AttackVector = Vector2.zero;
    // Use this for initialization
    private void Start () {
        delay = new WaitForSeconds(AttackDelay);
        InitializeProjectileList();

        fireCoroutine   = StartCoroutine(MonkeyFireProjectileBehaviour());
        patrolCoroutine = StartCoroutine(PatrolBehaviour(PatrolPoints[PatrolIndex].position));
    }


    private IEnumerator MonkeyFireProjectileBehaviour()
    {
        yield return delay;

        if (GameManager.GetInstance().gameState == GameState.GAME_OVER)
            yield return null;

        AttackVector = Abby.transform.position - (transform.position /*+ (Vector3)Abby.velocity/10f*/);

        FireProjectile(AttackVector);

        fireCoroutine = StartCoroutine(MonkeyFireProjectileBehaviour());
    }


    private IEnumerator PatrolBehaviour(Vector3 animationLocation)
    {
        canPatrol = true;

        float distance = (transform.position - animationLocation).magnitude;
        float duration = DurationMovement;

        Vector3 startingPos = transform.position;
        float t = 0;
        while (t < 1 && canPatrol)
        {
            t += Time.deltaTime / duration;
            transform.position = Vector3.Lerp(startingPos, animationLocation, t);
            yield return null;
        }

        if (!canPatrol)
            yield break;

        transform.position = animationLocation;
        IncrementMovementIndex();

        patrolCoroutine = StartCoroutine(PatrolBehaviour(PatrolPoints[PatrolIndex].position));

        yield return null;
    }

    private void IncrementMovementIndex()
    {
        PatrolIndex++;
        if(PatrolIndex == PatrolPoints.Length)
        {
            PatrolIndex = 0;
        }
    }
    private void InitializeProjectileList()
    {
        GameObject Projectile;
        for (int i = 0; i < ProjectilesCount; i++)
        {
            Projectile = Instantiate(MonkeyProjectile);
            Projectile.SetActive(false);
            ProjectileList.Add(Projectile);
        }
    }
    private void FireProjectile(Vector2 forceProjectile)
    {
        foreach (GameObject projectile in ProjectileList)
        {
            if (!projectile.activeInHierarchy)
            {
                projectile.transform.position = SpawnLocation.position;
                projectile.SetActive(true);
                projectile.GetComponent<TrailRenderer>().enabled = true;
                projectile.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
                projectile.GetComponent<Rigidbody2D>().AddForce(forceProjectile * (ProjectileSpeed + Random.Range(0, ProjectileSpeed/2)), ForceMode2D.Impulse);
                break;
            }
        }
    }

    private IEnumerator DoMovementCoroutine()
    {
        yield return new WaitForSeconds (0.01F);

        transform.localPosition = new Vector2(passivePosition, 0);

        yield return AnimatorExecutive.AnimatePositionCoroutine (gameObject, new Vector2 (activePosition, 0), 5.0F);

        fireCoroutine   = StartCoroutine(MonkeyFireProjectileBehaviour());
        patrolCoroutine = StartCoroutine(PatrolBehaviour(PatrolPoints[PatrolIndex].position));
    }



    private void OnCollisionEnter2D(Collision2D otherCollision)
    {
        if (otherCollision.gameObject.tag == "Projectile")
        {
            injures++;

            if (injures >= MAX_INJURES)
            {
                injures = 0;
                canPatrol = false;
                GetComponent<AudioSource>().Play();

                if(fireCoroutine != null)   StopCoroutine (fireCoroutine);
                if(patrolCoroutine != null) StopCoroutine (patrolCoroutine);

                movementCoroutine = StartCoroutine (DoMovementCoroutine());
            }
        }
    }
}
1

There are 1 answers

0
Ignacio Alorre On

With the information your provided, I would say the problem you may be facing is the GameObject you pass in the inspector to get the SpawnLocation has got a collider and a script with a OnCollisionEnter2D, which detect the projectil when you instantiate it and destroy it.

However, you are not destroying the projectil inside this OnCollisionEnter2D.

private void OnCollisionEnter2D(Collision2D otherCollision)
    {
        if (otherCollision.gameObject.tag == "Projectile")
        {
            injures++;

            if (injures >= MAX_INJURES)
            {
                injures = 0;
                canPatrol = false;
                GetComponent<AudioSource>().Play();

                if(fireCoroutine != null)   StopCoroutine (fireCoroutine);
                if(patrolCoroutine != null) StopCoroutine (patrolCoroutine);

                movementCoroutine = StartCoroutine (DoMovementCoroutine());
            }
        }
    }

In the case you dont have in your code any line to destroy the projectil gameobject after a collision. The problem could be you are not reaching this line projectile.SetActive(true);

I will try to replicate your code and check what may be happening