- Home /
 
               Question by 
               pastrana200 · Aug 02, 2014 at 08:55 AM · 
                noobball  
              
 
              Need help with begginner script!
I believe this is a very noob script. I just started programming in unity. :P So in this script i want a ball to jump when spacebar OR W is pressed.
 #pragma strict
 
 var rotationSpeed= 100;
 var jumpHeight = 8;
 
 private var isFalling = false;
 
 function Update () 
 {
     //Handles ball rotation.
     var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
     rotation *= Time.deltaTime;
     rigidbody.AddRelativeTorque (Vector3.back * rotation);
     
     if (Input.GetKeyDown(KeyCode.W) && isFalling == false)
     {
         rigidbody.velocity.y = jumpHeight;
     }
     isFalling = true;
     
     if (Input.GetKeyDown(KeyCode.Space) && isFalling == false)
     {
         rigidbody.velocity.y = jumpHeight;
     }
     isFalling = true;
 }
 
 function OnCollisionStay ()
 {
     isFalling = false;
 }
//The w key is working but not the spacebar. Why? Please help!
               Comment
              
 
               
              Answer by KiraSensei · Aug 02, 2014 at 08:57 AM
Try this :
 function Update () {
     //Handles ball rotation.
     var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
     rotation *= Time.deltaTime;
     rigidbody.AddRelativeTorque (Vector3.back * rotation);
 
     if (Input.GetKeyDown(KeyCode.W) && isFalling == false)
     {
         rigidbody.velocity.y = jumpHeight;
         isFalling = true;
     }
     if (Input.GetKeyDown(KeyCode.Space) && isFalling == false)
     {
         rigidbody.velocity.y = jumpHeight;
         isFalling = true;
     }
 }
or
 function Update () {
     var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
     rotation *= Time.deltaTime;
     rigidbody.AddRelativeTorque (Vector3.back * rotation);
  
     if ((Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W) || Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) && (isFalling == false))
     {
         rigidbody.velocity.y = jumpHeight;
         isFalling = true;
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Noob Question! Unexpected token: if. 1 Answer
Uhhhh... 2D Maze Generator Help! 3 Answers
Digital Clock: Time.time > 60 2 Answers
Where do I assign GUI scripts? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                