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.