Playing Audio After Destruction

What a wonderful Saturday to post some information, am I right?

Today I am focusing on how to play an audio clip after an enemy is destroyed. It took me a little bit to understand how it works so I thought I would post how I did it just in case this could potentially help someone.

So the scenario is that when the eyeball enemy in my game is getting hit I want it to play an audio clip (hit noise)  and then after the eyeball is destroyed I want it to play another clip (destruction).

The Enemy Object Setup

  1. First created a parent object called Eyeball
  2. Then, I attached an Audio Source Component to it and left the AudioClip blank

3. Next, I created a child object inside of the parent and also named it Eyeball (confusing I know but this was a workaround that I will probably update later, or not…we shall see) and attached an Eyeball Controller script to it.

4. Inside the script, I have a reference to the AudioSource that is on the parent object and I also have a reference to two Audio Cips: Enemy Hit (when the enemy is hit), and Enemy Destroyed (when the enemy is destroyed).

The Eyeball Controller Setup

I have stripped everything out of this code that isn’t audio related to the topic I am talking about right now.

  1. In the script you can see there is a currentHealth variable for the enemy as well as the three other components we talked about earlier (an AudioSource and two AudioClips).
  2. Then, in the Start Method, you assign the enemyAudioSource.
  3. There is a Method called TakeDamage which I give to my PlayerController. This script is used when the collider from the player collides with the enemy and causes damage. For this example lets say 5 damage is given to this Method, so now damageAmount = 5.
  4. When the TakeDamage Method is fired you can see that I assign the enemyAudioSource.clip to equal the enemyHit SoundClip. The enemyAudioSource then begins to play (this will be the enemy being hit sound).
  5. The currentHealth of the enemy is then reduced by the damageAmount.
  6. If  the currentHealth is <= 0, the enemyAudioSource.clip is replaced with the enemyDestroyed AudioClip. The enemyAudioSource then begins to play (this will be the enemy being destroyed sound).
  7. The Method for DestroyEnemy() begins which just destroys the enemy child object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EyeballController : MonoBehaviour {

    [Header("-----Health-----")]
    public int currentHealth;

    [Header("-----Sounds-----")]
    public AudioSource enemyAudioSource;
    public AudioClip enemyHit;
    public AudioClip enemyDestroyed;

    private void Start()
    {
        enemyAudioSource = GetComponentInParent&lt;AudioSource&gt;();
    }

    public void TakeDamage(int damageAmount)
    {
        enemyAudioSource.clip = enemyHit;
        enemyAudioSource.Play();

        currentHealth -= damageAmount;

        if(currentHealth &lt;= 0)
        {
            enemyAudioSource.clip = enemyDestroyed;
            enemyAudioSource.Play();

            DestroyEnemy();
        }
    }

    void DestroyEnemy()
    {
        Destroy(gameObject);
    }
}

Back To The Eyeball Parent

The last thing I did was attach a script called DestroyEnemyContainer to the Eyeball Parent. The script is very simple and will just look to see if there are any children attached to the parent and if there is not it will destroy the parent after 3 seconds.


public class DestroyParentContainer : MonoBehaviour
{
private void Update()
{
//Debug.Log("Number of children: " + transform.childCount);

if (transform.childCount <= 0) // if this object is the last child
{
Destroy(transform.gameObject, 3.0f); // destroy parent a few frames later
}
}
}

Face and Move Toward Player.

Good Morning All,

I am not a programming expert by any stretch of the imagination, but if there is one thing I know it’s that getting the code to work is the first step and optimizing is the second step.

With that being said I have decided to share all the code I am using for my current project and hope it can help someone or someone can help me make it better. Regardless, the game I am working on is a Platformer/RPG so there will be a lot of systems happening.

The first code piece I would like to share is the MoveTowardPlayer script that determines if the player is on the Left or Right of whatever GameObject this script is attached to and will move the host toward the player. If the player crosses its path, the enemy will flip its X-Axis to face the player.

 

Below is the script I use to accomplish this effect. 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveTowardPlayer : MonoBehaviour {

public Transform target;
public float speed;

private void Start()
{

//This finds the GameObject that has the PlayerController script attached.
//This is assuming there is only one PlayerController.
target = FindObjectOfType<PlayerController>().transform;
}

void Update()
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);

#region \/-----Face Right-----\/
if(transform.position.x < target.position.x)
{
GetComponent<SpriteRenderer>().flipX = true;
}
#endregion
#region \/-----Face Left-----\/
else if(transform.position.x > target.position.x)
{
GetComponent<SpriteRenderer>().flipX = false;
}
#endregion
}
}

***As of 04/13/18, this is the current script I am using. If I update it I will update this post.***

 

Variable Jump Height

I have been working on my latest game, and I ran into an issue with jumping. The issue is that when the player presses the jump button, in my case the A button on the controller, I wanted the character to jump. By releasing the jump button instead of holding it down I wanted the character to jump short of the max height that they could reach.

Well, I found a solution that I was able to incorporate into my project and I wanted to share the solution with you in case you were also running into this issue.

Below is a post I found on the unity3D subreddit from the user dookie-boy where he talks about how he accomplished this feat.