- Home /
Roll A Ball Sound
So I finished the roll a ball tutorial now im trying to figure out how to play sounds when the ball is rolling, hits the wall with different pitches, and to play a you win! sound when all 12 have been collected. Ive surfed the internet for a while and nothing hasn't been able to work other than coin pick up sounds if anyone doesn't mind explaining how to do this that would be appreciated. Here is the script I have for the player(ball).
Thanks in advanced!
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
private AudioSource myAudio;
void Start ()
{
rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText ();
winText.text = "";
myAudio = GetComponent<AudioSource> ();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up")) {
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
myAudio.Play ();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString ();
if (count >= 12)
{
winText.text = "You Win!";
}
}
}
Answer by Prastiwar · Jan 26, 2018 at 08:19 AM
Hey, if you got idea how to play sound on pickup, what's the problem? It's clearly logic stuff, just ask yourself question and answer it: You want to play sound:
when player wins, so where do you have win condition? You have it already, just put play sound in it. If you would to have different AudioClip for it, change it by this method
when player hits the wall, so you need condition to check collision for hitting wall. You can achieve this by OnCollisionEnter method. Mechanic would be very similiar to your trigger, but you want to check for wall, not for pick up (and obviously you won't increment "count"). Just before playing sound you want to change pitch, so do it. And remember to have properly tag on wall objects.
Thanks a lot! I was able to get it to work. I guess I was just overlooking it haha
Your answer
Follow this Question
Related Questions
Microphone to Gameplay mechanics 1 Answer
TOD script editing (sounds) 0 Answers
cant integrate unity 5 wwise 2016.2 0 Answers
3D sound too low 1 Answer
PlaySound Once After Activated 1 Answer