- Home /
Question by
Doozku · Jun 08, 2015 at 10:12 AM ·
unity 5javascriptplayer movement
Ball Rolling Forever
Hello, I am learning how to work with unity and I was making a game with a ball that you roll around. It was working fine but when I added sound the project there was a logic error that happened. Before when I stopped pressing the D key it would stop the ball from rolling. But when I added sound the ball keeps rolling in the direction you entered even when you stop pressing the button. I was following the Brackeys tutorial and I downloaded his version but the same thing happens. Here is the source code. It is in javascript. It is the script that handles the way that the ball moves.
var rotationSpeed = 100;
var jumpHeight = 8;
var Hit01 : AudioClip;
var Hit02 : AudioClip;
var Hit03 : AudioClip;
var distToGround : float;
function Start () {
// Getting the distance from the center to the ground.
distToGround = GetComponent.<Collider>().bounds.extents.y;
}
function Update ()
{
//Handle ball rotation.
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);
//Ball jump
if (Input.GetKeyDown(KeyCode.W) && IsGrounded ())
{
GetComponent.<Rigidbody>().velocity.y = jumpHeight;
}
}
function IsGrounded () : boolean { //Check if we are on the ground. Return true if we are else return null.
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
function OnCollisionEnter () {
var theHit = Random.Range(0, 3);
if (theHit == 0)
{
GetComponent.<AudioSource>().clip = Hit01;
}
else if (theHit == 1)
{
GetComponent.<AudioSource>().clip = Hit02;
}
else {
GetComponent.<AudioSource>().clip = Hit03;
}
GetComponent.<AudioSource>().pitch = Random.Range (0.9,1.1);
GetComponent.<AudioSource>().Play();
}
Thank you for your help.
Comment