Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Making a Sphere jump

Discussion in 'Scripting' started by Pituli, Jun 3, 2012.

Thread Status:
Not open for further replies.
  1. Pituli

    Pituli

    Joined:
    May 31, 2012
    Posts:
    31
    Hello, i am making a game with a sphere and i would like to know what's the code to make the sphere jump. I also need it to play a sound when it jumps... Thanks
     
  2. henry96

    henry96

    Joined:
    Sep 28, 2011
    Posts:
    582
    Don't mind me if I'm saying this. But have you followed, watched, or read any tutorial?
     
    BobbyGG likes this.
  3. Pituli

    Pituli

    Joined:
    May 31, 2012
    Posts:
    31
    Yes, i have followed 2 tutorials, but neither had the jump code, i also attended a live lesson on how to use unity but they didn't show how to make something jump either, and i looked it up on the Unity Script Reference and i didn't find it.
     
  4. danyfalty

    danyfalty

    Joined:
    Jun 3, 2012
    Posts:
    12
    Hey,

    First add a Rigid Body to give the Object physics properties.
    Component>Physics>Rigid Body.
    And place your cube above the ground so it can fall on the ground.
    I know this isn't what you had in mind but i hope its good enough.
    Now Add This Java script code to you Cube.

    Function Update(){
    rigidbody.AddForce(0,10,0);
    }
     
    Last edited: Jun 3, 2012
  5. henry96

    henry96

    Joined:
    Sep 28, 2011
    Posts:
    582
    For basic jump, I would use :


    Code (csharp):
    1. function Update () {
    2.  
    3. if (Input.GetButtonUp ("Fire1")){ //Release left click
    4.  
    5. transform.Translate (Vector3 (0,5.0,0));
    6.  
    7. }
    However, we never use something like this when we develop game seriously. Most people prefer to use CharacterControl to do so cuz it's much more convenient. By the way, for my current project, I can't use characterControl because it's physics based, so I'm stuck with the method similar to the code I wrote. :D
     
  6. Pituli

    Pituli

    Joined:
    May 31, 2012
    Posts:
    31
    I have wasd keys to move, and i want to change it to instead of w being forward, being jumping, unity says that the scripts you guys gave me have errors, i changed the moving forward line to make it jumping... what i have is this:

    function FixedUpdate ()
    {
    if (Input.GetKey ("w")) rigidbody.AddForce (Vector3.up * speed * deltaTime);
    }

    This will make the character go up, but if you keep pressing the key, it will keep going up... i need it to go up and then go back down, i tried to make it Input.GetKeyDown or GetKeyUp but it doesn't do anything. How can i get it to go down from here into the ground....
     
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  8. Pituli

    Pituli

    Joined:
    May 31, 2012
    Posts:
    31
    I am new to JavaScript and i kinda understand it, but i can't write it.... i don't get what i am supposed to do with grounded....
     
  9. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    you could just use the first person controller prefab or write a script yourself. In order to get it to jump properly, you need to have a boolean that checks if its on the ground on or not. If its not, then the jump key does not work. If it is, then it does. here is a simple code that does it. Just add a rigid body to the character and attach this script.

    Code (csharp):
    1. #pragma strict
    2.  
    3. var onGround : boolean = false;
    4.  
    5.  
    6. function Update () {
    7.  
    8.     if(Input.GetKeyDown("space")  onGround == true)
    9.     {
    10.         rigidbody.AddForce(0,300,0);
    11.     }
    12. }
    13.  
    14. function OnCollisionEnter (other : Collision)
    15. {
    16.  
    17. //checks to make sure the collision we collided with is the ground.
    18. //You will need to tag the terrain as ground
    19.     if(other.gameObject.tag == "ground")
    20.     {
    21.         print("we are on the ground");
    22.         onGround = true;
    23.     }
    24. }
    25.  
    26. function OnCollisionExit (otherExit : Collision)
    27. {
    28.  
    29.     //checks to make sure the collision we are no longer colliding with is the ground
    30.     if(otherExit.gameObject.tag == "ground")
    31.     {
    32.         print("we are off the ground");
    33.         onGround = false;
    34.     }
    35.  
    36. }
     
  10. Pituli

    Pituli

    Joined:
    May 31, 2012
    Posts:
    31
    Thanks a lot, it worked just how i wanted it. Thanks
     
  11. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    alright. The point for writing the code was for you to examine it and see how it works. Such as the OnCollisionEnter/Exit functions. And how the if statement in the update function cheks if the spacebar is pressed AND onGround is true.
     
  12. [DJ]Creepz

    [DJ]Creepz

    Joined:
    Dec 23, 2011
    Posts:
    136
    sir.. i have the same problem...
    i tried your code in C#

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent (typeof(CharacterController))]
    6. public class PlayerControl : MonoBehaviour {
    7.     private float moveSpeed = 8;
    8.     private float sideSpeed = 10;
    9.     private bool jumpED = false;
    10.    
    11.    
    12.     private Transform _myTransform;
    13.     private CharacterController _myController;
    14.    
    15.     // Use this for initialization
    16.    
    17.     void Awake(){
    18.         _myTransform = transform;
    19.         _myController = GetComponent<CharacterController>();
    20.     }
    21.    
    22.     void Start () {
    23.         animation.wrapMode = WrapMode.Loop;
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.         if(!_myController.isGrounded){
    29.             _myController.Move(Vector3.down * Time.deltaTime);
    30.         }
    31.        
    32.         Walk ();
    33.         SidetoSide();
    34.        
    35.         if(Input.GetKeyDown("space")  jumpED == true){
    36.             Debug.Log("what?");
    37.             rigidbody.AddForce(0,300,0);
    38.         }
    39.     }
    40.    
    41.     private void Walk(){
    42.         if(Mathf.Abs(Input.GetAxis("Up-down")) > 0){
    43.             if(Input.GetButton("Up-down")){
    44.                 _myController.SimpleMove(_myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("Up-down") * moveSpeed);
    45.                 animation.CrossFade("walk");
    46.             }
    47. //          else{
    48. //              if((Input.GetAxis("Up-down")) < 0){
    49. //                  animation.CrossFade("walk");
    50. //                  _myController.SimpleMove(_myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("Up-down") * moveSpeed);
    51. //              }
    52. //              else{
    53. //                  animation.CrossFade("walk");
    54. //                  _myTransform.Rotate(0,Input.GetAxis("Up-down") * Time.deltaTime,0);
    55. //                  _myController.SimpleMove(_myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("Up-down") * moveSpeed);
    56. //              }
    57. //          }
    58.         }
    59.         else
    60.         {
    61.             animation.CrossFade("idle");
    62.         }
    63.     }
    64.    
    65.     private void SidetoSide(){
    66.         if(Mathf.Abs(Input.GetAxis("Sidestep")) > 0){
    67.             if(Mathf.Abs(Input.GetAxis("Sidestep")) > 0)
    68.             {
    69.                 _myController.SimpleMove(_myTransform.TransformDirection(Vector3.left) * Input.GetAxis("Sidestep") * sideSpeed);
    70.             }
    71.             else{
    72.                 _myController.SimpleMove(_myTransform.TransformDirection(Vector3.right) * Input.GetAxis("Sidestep") * sideSpeed);
    73.                
    74.             }
    75.         }  
    76.     }
    77.    
    78.    
    79.     void OnCollisionEnter (Collision other){
    80.         if(other.gameObject.tag == "Ground"){
    81.             Debug.Log("We are in land");
    82.             jumpED = true;
    83.         }
    84.     }
    85.    
    86.     void OnCollisionExit (Collision otherExit){
    87.         if(otherExit.gameObject.tag == "Ground"){
    88.             Debug.Log("We are in air");
    89.             jumpED = false;
    90.         }
    91.     }
    92. }
    93.  
    but when i'd start it.. it doesn't goes up like jumping..
    is there something missing in my code?
     
  13. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    This is because you are applying physics to a character controller. Character Controller's do not use physics.

    This is an example of a Character controlled Character with a jump:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.     // generic controls
    7.     float speed = 8;
    8.     float jumpSpeed = 12;
    9.    
    10.     // character controller
    11.     CharacterController controller;
    12.     public Vector3 move;
    13.     public bool grounded = false;
    14.    
    15.     // camera control
    16.     float x=0;
    17.     float y=0;
    18.    
    19.     // Use this for initialization
    20.     void Start () {
    21.         controller = GetComponent<CharacterController>();
    22.         if(!controller)
    23.             controller = gameObject.AddComponent<CharacterController>();
    24.        
    25.         Vector3 angles = Camera.main.transform.eulerAngles;
    26.         x = angles.y;
    27.         y = angles.x;
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.         Vector3 input = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis("Vertical"));
    33.         input = transform.TransformDirection(input);
    34.         if(input.magnitude > 1) input.Normalize();
    35.         float gravity = Physics.gravity.y * 3;
    36.        
    37.         if(grounded){
    38.             move = input * speed;
    39.             move.y = gravity * Time.deltaTime;
    40.            
    41.             if(Input.GetButtonDown("Jump")) move.y = jumpSpeed;
    42.         } else {
    43.             move.x = Mathf.Clamp(move.x + input.x * 0.01f * speed, -speed, speed);
    44.             move.y = Mathf.Clamp(move.y + gravity * Time.deltaTime, gravity, jumpSpeed);
    45.             move.z = Mathf.Clamp(move.z + input.z * 0.01f * speed, -speed, speed);
    46.         }
    47.        
    48.         transform.rotation = Camera.main.transform.rotation;
    49.         Vector3 euler = transform.eulerAngles;
    50.         euler.x = 0;
    51.         transform.eulerAngles = euler;
    52.        
    53.         CollisionFlags flags = controller.Move(move * Time.deltaTime);
    54.         grounded = (flags  CollisionFlags.Below) > 0;
    55.     }
    56.    
    57.     void LateUpdate(){
    58.         // quick mouse orbit
    59.         x += Input.GetAxis("Mouse X") * 5;
    60.         y -= Input.GetAxis("Mouse Y") * 5;
    61.        
    62.         if(y > 180) y -= 360;
    63.         if(y < -180) y += 360;
    64.         y = Mathf.Clamp(y, -60,60);
    65.        
    66.         Camera.main.transform.rotation = Quaternion.Euler(y,x,0);
    67.         Camera.main.transform.position = Camera.main.transform.rotation * new Vector3(0, 0, -3) + transform.position;
    68.     }
    69. }
    70.  
     
  14. [DJ]Creepz

    [DJ]Creepz

    Joined:
    Dec 23, 2011
    Posts:
    136
    jesus.. this makes my head hurt.. XD
    i don't understand this part
    Code (csharp):
    1.  
    2.  void Update () {
    3.  
    4.         Vector3 input = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis("Vertical"));
    5.  
    6.         input = transform.TransformDirection(input);
    7.  
    8.         if(input.magnitude > 1) input.Normalize();
    9.  
    10.         float gravity = Physics.gravity.y * 3;
    11.  
    12.        
    13.  
    14.         if(grounded){
    15.  
    16.             move = input * speed;
    17.  
    18.             move.y = gravity * Time.deltaTime;
    19.  
    20.            
    21.  
    22.             if(Input.GetButtonDown("Jump")) move.y = jumpSpeed;
    23.  
    24.         } else {
    25.  
    26.             move.x = Mathf.Clamp(move.x + input.x * 0.01f * speed, -speed, speed);
    27.  
    28.             move.y = Mathf.Clamp(move.y + gravity * Time.deltaTime, gravity, jumpSpeed);
    29.  
    30.             move.z = Mathf.Clamp(move.z + input.z * 0.01f * speed, -speed, speed);
    31.  
    32.         }
    33.  
    34.        
    35.  
    36.         transform.rotation = Camera.main.transform.rotation;
    37.  
    38.         Vector3 euler = transform.eulerAngles;
    39.  
    40.         euler.x = 0;
    41.  
    42.         transform.eulerAngles = euler;
    43.  
    44.        
    45.  
    46.         CollisionFlags flags = controller.Move(move * Time.deltaTime);
    47.  
    48.         grounded = (flags  CollisionFlags.Below) > 0;
    49.  
    50.     }
    51.  
    52.  
     
  15. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    documented...
    Code (csharp):
    1.  
    2.     void Update () {
    3.         // collect th inputs (wasd)
    4.         Vector3 input = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis("Vertical"));
    5.        
    6.         // transform the inputs into the direction the character is facing
    7.         input = transform.TransformDirection(input);
    8.        
    9.         // if the inputs are 1 and 1, make them smaller so that you dont
    10.         // run faster diagnaly
    11.         if(input.magnitude > 1) input.Normalize();
    12.        
    13.         // capture gravity from physics.
    14.         float gravity = Physics.gravity.y * 3;
    15.        
    16.         // if we are grounded
    17.         if(grounded){
    18.             // move is set simply to the input times the speed
    19.             move = input * speed;
    20.             // make sure to add a little gravity to prevent constant falling down slopes
    21.             move.y = gravity * Time.deltaTime;
    22.            
    23.             // if we press the jump key, make our vertical movement the jump speed
    24.             if(Input.GetButtonDown("Jump")) move.y = jumpSpeed;
    25.         } else {
    26.             // if we are not grounded
    27.             // add a fraction of the movement we are pressing to our current movement
    28.             // make sure to clamp it down where you cant jump faster than you can run
    29.             move.x = Mathf.Clamp(move.x + input.x * 0.01f * speed, -speed, speed);
    30.             move.y = Mathf.Clamp(move.y + gravity * Time.deltaTime, gravity, jumpSpeed);
    31.             move.z = Mathf.Clamp(move.z + input.z * 0.01f * speed, -speed, speed);
    32.         }
    33.        
    34.         // set the rotation the same as the camera
    35.         transform.rotation = Camera.main.transform.rotation;
    36.         // change the x euler angle to zero so that we dont look down or up
    37.         Vector3 euler = transform.eulerAngles;
    38.         euler.x = 0;
    39.         transform.eulerAngles = euler;
    40.        
    41.         // Do the move
    42.         CollisionFlags flags = controller.Move(move * Time.deltaTime);
    43.         // collect the grounded information
    44.         grounded = (flags  CollisionFlags.Below) > 0;
    45.     }
    46.  
     
  16. [DJ]Creepz

    [DJ]Creepz

    Joined:
    Dec 23, 2011
    Posts:
    136
    thank you sir for helping me..now i got this jump
    but something ain't right.. the normal forward movement speed of my char is 4f and my sideward speed is 5f.. but when I'd place your code..
    i think it was multiplier.. because my character goes faster when moving forward and backward than the sideways..
     
  17. [DJ]Creepz

    [DJ]Creepz

    Joined:
    Dec 23, 2011
    Posts:
    136
    @BigMisterB
    nah... i figured it out..
    thanks again for the help
     
  18. Ogeid

    Ogeid

    Joined:
    Apr 16, 2013
    Posts:
    5
    Simple and easy!

    public class PlayerController : MonoBehaviour {

    public float speed;

    private Rigidbody rb;

    void Start()
    {
    rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");


    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    if (Input.GetKey (KeyCode.Space)) movement = new Vector3 (moveHorizontal, 2, moveVertical);




    rb.AddForce(movement * speed);
    }


    }
     
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please do not resurrect old and buried threads. Please look at the date of the original post and the last post.

    If the thread is inactive: Do Not Post.

    Please create a new thread of your own. If you feel you need to reference the old post, do so as a link.

    As this is an undead thread, I am closing it.
     
    LeftyRighty likes this.
Thread Status:
Not open for further replies.