Solo Challenge - thinning the fog

Well, I enjoyed this challenge a lot. It pushed me but also did not make me feel totally out of my depth, its one oft he moments where I have actually felt that, yeah, I will be able to code soon.
Anyway here is how I did it:
Tower script:
public class Tower : MonoBehaviour

{
[SerializeField] Transform objectToPan;
[SerializeField] Transform targetEnemy;
[SerializeField] Transform enemyBase;
[SerializeField] GameObject bullets;
[SerializeField] float range = 20;
// Start is called before the first frame update

// Update is called once per frame
void Update()
{

    CheckForValidTargets();
}

private void CheckForValidTargets()
{
    if (targetEnemy == null)
    {

    }
    else
    {
        TowerAquiresTargetIfInRange();
    }
}

private void TowerAquiresTargetIfInRange()
{
    float distance = Vector3.Distance(targetEnemy.position, objectToPan.position);
    if (distance <= range || targetEnemy != null)
    {
        LookAtEnemy();
        print("InRange");
        ShootEnemy(true);
    }
    else
    {
        print("Out Of Range");
        ShootEnemy(false);
        //Todo turn on red light
        objectToPan.LookAt(enemyBase); //Should this be in its own function?
    }
}

private void ShootEnemy(bool isActive)
{
    var emissionModule = bullets.GetComponent<ParticleSystem>().emission;
    emissionModule.enabled = isActive;
}

private void LookAtEnemy()
{
    objectToPan.LookAt(targetEnemy);
}

}

Possible additions: It would like to have more feedback to the player to show that towers have a target or not, this would involve turning on lights to telegraph this.

Possible problems: This is set up to deal with only one enemy that’s added to the serialised field box. The enemy keeps shooting if the target is destroyed (tried to fix that with this:
“(distance <= range || targetEnemy != null)”
The particles bounce off the enemy.

Here is my enemy health script:

public class EnemyHealth : MonoBehaviour
{
[SerializeField] int hits = 10;
[Tooltip(“FX Prefab on player”)] [SerializeField] GameObject deathFX;
[SerializeField] Transform parent;
// Start is called before the first frame update
void Start()
{
//AddBoxCollider();
}

//private void AddBoxCollider()
//{
    //Collider boxCollider = gameObject.AddComponent<BoxCollider>();
    //boxCollider.isTrigger = false;
//}
// Update is called once per frame
void Update()
{
    
}
void OnParticleCollision(GameObject other)
{
    ProccessHits();
    if (hits <= 0)
    {
        KillEnemy();
    }
}
private void ProccessHits()
{
hits = hits - 1;
    print("Target Hit!");    
}

private void KillEnemy()
{
 GameObject fx = Instantiate(deathFX, transform.position, Quaternion.identity);
 fx.transform.parent = parent;
 Destroy(gameObject);
}

}

I pretty much nabbed this from the Argon assault project and stripped out the score code. It still felt like I was learning by using this code and working out what it all did etc. I had an issue with adding the box collider via code, I could not really remember why that was done. The script is on the empty parent of the enemy so the box collider is tiny! This means the particles always missed, so I just manually added and edited the collider.

Thanks!

I ended up adding the light colour change stuff to this script!
Was really hyped to get it working!

public class Tower : MonoBehaviour
{
[SerializeField] Transform objectToPan;
[SerializeField] Transform targetEnemy;
[SerializeField] Transform enemyBase;
[SerializeField] GameObject bullets;
[SerializeField] float range = 20;
[SerializeField] GameObject targetLight;

// Update is called once per frame
void Update()
{
    if (targetEnemy)
    {
        TowerAquiresTargetIfInRange();
    }
    else
    {
        objectToPan.LookAt(enemyBase);//Should this be in its own function? - Using this twice now
        ShootEnemy(false);
    }
}
private void TowerAquiresTargetIfInRange()
{
    float distance = Vector3.Distance(targetEnemy.position, objectToPan.position);
    if (distance <= range) 
    {
        LookAtEnemy();
        print("InRange");
        ShootEnemy(true);
        **TurnOnGreenLight();**
    }
    else
    {
        print("Out Of Range");
        ShootEnemy(false);
        **TurnOnRedLight();**
        objectToPan.LookAt(enemyBase);//Using this twice now
    }
}

private void TurnOnRedLight()
{
    var targetLightRenderer = targetLight.GetComponent<Renderer>();
    targetLightRenderer.material.SetColor("_EmissionColor", Color.red);
    **print("Red Light");**
}

private void TurnOnGreenLight()
{
    var targetLightRenderer = targetLight.GetComponent<Renderer>();
    targetLightRenderer.material.SetColor("_EmissionColor", Color.green);
}

private void ShootEnemy(bool isActive)
{
    var emissionModule = bullets.GetComponent<ParticleSystem>().emission;
    emissionModule.enabled = isActive;
}

private void LookAtEnemy()
{
    objectToPan.LookAt(targetEnemy);
}

}

I really feel this challenge has pushed me to start doing stuff on my own! Which feels really good!

Thanks again.

Privacy & Terms