Help with Sound tutorial

Hello, basically I’m completely new to game development with very minor experience in programming and a good amount of experience in 3D objects. Currently, I am following a tutorial by Brackys on Youtube

in which I made this code:

#pragma strict

var rotationSpeed = 100;
var jumpHeight = 8;
private var playOnce = false;

var hit01 : AudioClip;
var hit02 : AudioClip;
var hit03 : AudioClip;

private var isFalling = false;

function Update ()
{
    //Handle Ball Rotation.
    var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
    rotation *=Time.deltaTime;
    GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);

    if (Input.GetKeyDown (KeyCode.W) && isFalling == false)
    {
       GetComponent.<Rigidbody>().velocity.y = jumpHeight;
       playOnce = true;
       playOnceTrue ();
       Debug.Log("Check1");
       isFalling = true;
    }
}

function OnCollisionStay ()
{
    if (playOnce == true)
        Debug.Log("Check2");
    {
        var theHit = Random.Range (0, 3);
        playOnce = false;
        if (theHit == 0)
        {
            GetComponent.<AudioSource>().clip = hit01;
        }
        else if (theHit == 1)
        {
            GetComponent.<AudioSource>().clip = hit02;
        }
        else if (theHit == 2)
        {
            GetComponent.<AudioSource>().clip = hit03;
        }
        GetComponent.<AudioSource>().Play();
        Debug.Log("Check 2.5");
    }
    isFalling = false;
    Debug.Log("Check 2.8");
}

function playOnceTrue ()
{
    yield WaitForSeconds (0.3);
    var playOnce = true; 
    Debug.Log("Check3");
}

But for some reason, the code triggers everytime the ball rotates instead of when it first touches the box. I understand the tutorial is out of date so any help would be appreciated in what’s wrong as I have tried debugging it as well and from debugging, Checks 1, 2 and 3 trigger on button press only.

Thanks

Maybe try OnCollisionEnter instead of OnCollisionStay.

Or, if you want to be like the video, take “var” out of that last function, playOnceTrue().

Right now, I think it is just creating a local variable that does pretty much nothing.

Have a blessed night.

Tried that, even OnCollisionExit, neither works so I’m trying a different tutorial now, thanks for the help though

I think you can download the code right here:

http://brackeys.com/preview/make-a-game/

I downloaded it and looked at it.

There are definitely differences between your code and what’s in the zip.

I believe it’s BallControl.js in the Video11 folder.

Have a good day.

1 Like