Whenever I change direction, the ball I am controlling goes out of control and starts moving in the wrong
Here is the code I use to move:
#pragma strict
var rotationSpeed = 280;
var jumpHeight = 8;
var distToGround : float;
var hitOne : AudioClip;
var hitTwo : AudioClip;
var hitThree : AudioClip;
function Start ()
{
//Getting the distance from the center of the ball to the ground.
distToGround = collider.bounds.extents.y;
}
function Update ()
{
//Handles ball rotation to move.
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation*= Time.deltaTime;
rigidbody.AddRelativeTorque (Vector3.back * rotation);
if(Input.GetKeyDown(KeyCode.W) IsGrounded ())
{
rigidbody.velocity.y = jumpHeight;
}
}
function IsGrounded () : boolean
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
function OnCollisionEnter ()
{
var theHit = Random.Range(0, 3);
if (theHit == 0)
{
audio.clip = hitOne;
}
else if (theHit == 1)
{
audio.clip = hitTwo;
}
else
{
audio.clip = hitThree;
}
audio.pitch = Random.Range(0.9, 1.4);
audio.Play();
}
This is pretty much breaking the game because it makes it near-impossible to move past the second level.