- Home /
how to add sound to unity
How do I code the script to make a sound when the knife hits an object and after a failed attempt? This is my script to run the entire game:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(GameUI))]
public class GameController : MonoBehaviour
{
public static GameController Instance { get; private set; }
[SerializeField]
private int knifeCount;
[Header("Knife Spawning")]
[SerializeField]
private Vector2 knifeSpawnPosition;
[SerializeField]
private GameObject knifeObject;
public GameUI GameUI { get; private set; }
private void Awake()
{
Instance = this;
GameUI = GetComponent<GameUI>();
}
private void Start()
{
GameUI.SetInitialDisplayedKnifeCount(knifeCount);
SpawnKnife();
}
public void OnSuccessfulKnifeHit()
{
if (knifeCount > 0)
{
SpawnKnife();
}
else
{
StartGameOverSequence(true);
}
}
private void SpawnKnife()
{
knifeCount--;
Instantiate(knifeObject, knifeSpawnPosition, Quaternion.identity);
}
public void StartGameOverSequence(bool win)
{
StartCoroutine("GameOverSequenceCoroutine", win);
}
private IEnumerator GameOverSequenceCoroutine(bool win)
{
if (win)
{
yield return new WaitForSecondsRealtime(0.3f);
FindObjectOfType<LevelLoader>().LoadNextLevel();
}
else
{
GameUI.ShowRestartButton();
}
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
}
}
and this is the script for the knife:
using UnityEngine;
public class KnifeScript : MonoBehaviour
{
[SerializeField]
private Vector2 throwForce;
//knife shouldn't be controlled by the player when it's inactive
//(i.e. it already hit the log / another knife)
private bool isActive = true;
//for controlling physics
private Rigidbody2D rb;
//the collider attached to Knife
private BoxCollider2D knifeCollider;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
knifeCollider = GetComponent<BoxCollider2D>();
}
void Update()
{
//this method of detecting input also works for touch
if (Input.GetMouseButtonDown(0) && isActive)
{
//"throwing" the knife
rb.AddForce(throwForce, ForceMode2D.Impulse);
//once the knife isn't stationary, we can apply gravity (it will not automatically fall down)
rb.gravityScale = 1;
//Decrement number of available knives
GameController.Instance.GameUI.DecrementDisplayedKnifeCount();
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
//we don't even want to detect collisions when the knife isn't active
if (!isActive)
return;
//if the knife happens to be active (1st collision), deactivate it
isActive = false;
//collision with a log
if (collision.collider.tag == "Log")
{
//play the particle effect on collision,
//you don't always have to store the component in a field...
GetComponent<ParticleSystem>().Play();
//stop the knife
rb.velocity = new Vector2(0, 0);
//this will automatically inherit rotation of the new parent (log)
rb.bodyType = RigidbodyType2D.Kinematic;
transform.SetParent(collision.collider.transform);
//move the collider away from the blade which is stuck in the log
knifeCollider.offset = new Vector2(knifeCollider.offset.x, -0.4f);
knifeCollider.size = new Vector2(knifeCollider.size.x, 1.2f);
//Spawn another knife
GameController.Instance.OnSuccessfulKnifeHit();
}
//collision with another knife
else if (collision.collider.tag == "Knife")
{
//start rapidly moving downwards
rb.velocity = new Vector2(rb.velocity.x, -2);
//Game Over
GameController.Instance.StartGameOverSequence(false);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Gunfire Sound Gets Cut Off When I Release the Aim Button. Help? 2 Answers
How do I change the size of an object depending on the sound in unity 2D? 0 Answers
3D sound in topdown game heard in only one hear. 1 Answer
How to change the overlapping Game Object position ? 0 Answers
How to activate a sound when a player walks through a collider 1 Answer