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

i need help with my code for jumping

Discussion in 'Scripting' started by itspicklerickandmorty, Apr 7, 2020.

  1. itspicklerickandmorty

    itspicklerickandmorty

    Joined:
    Apr 6, 2020
    Posts:
    16
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Moveball : MonoBehaviour
    6. {
    7.     Rigidbody rb;
    8.     public int ballspeed = 0;
    9.     public int jumpspeed = 0;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         float Hmove = Input.GetAxis ("Horizontal");
    20.         float Vmove = Input.GetAxis ("Vertical");
    21.  
    22.         Vector3 ballmove = new Vector3(Hmove, 0.0f, Vmove);
    23.         rb.AddForce (ballmove * ballspeed);
    24.  
    25.         if(Input.GetKey(KeyCode.Space));
    26.         {
    27.             Vector3 balljump = new Vector3 (0.0f, 0.0f, 0.0f);
    28.             rb.AddForce(balljump * ballspeed);
    29.  
    30.         }
    31.     }
    32. }
    33.  
    is there anything wrong with any of the code for the ball to jump if there is anything wrong with the code for jumping please edit it so I can see what I did wrong with my code and
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't understand what you're doing on lines 27 & 28. With balljump being (0f,0f,0f) then the math on line 28 will always result in a force of 0f, so why even try adding a 0f force? 0 times any number is always 0 of course.

    Don't add forces in Update. Add them in FixedUpdate. But keep your input checking in Update.
     
  3. itspicklerickandmorty

    itspicklerickandmorty

    Joined:
    Apr 6, 2020
    Posts:
    16
    I'm trying to get the ball to jump which is not working could you suggest a solution to this problem I'm having with it not jumping and possibly write some code to make the object jump
     
    Last edited: Apr 7, 2020
  4. LatchGameDev

    LatchGameDev

    Joined:
    Nov 24, 2014
    Posts:
    61
    Code (CSharp):
    1. if(Input.GetKey(KeyCode.Space));
    2.         {
    3.             Vector3 balljump = new Vector3 (0.0f, 0.0f, 0.0f);
    4.             rb.AddForce(balljump * ballspeed);
    5.         }
    Well here you are setting ballJump to zero on xyz. So when you AddForce the calculation looks like
    Force is now Zero * BallSpeed. So instead i'd put Vector3 balljump = new Vector3 (0.0f, 1.0f, 0.0f); to say i'd want it to go up in the Y direction.

    Also I assume you set ballSpeed and jumpSpeed to something other then zero in your inspector? I'd probably change those lines to

    Code (CSharp):
    1. public int ballspeed = 1;
    2. public int jumpspeed = 1;
    Just to make sure the default value still does something / shows the behavior of the script.

    Code (CSharp):
    1. if(Input.GetKey(KeyCode.Space));
    I'd also change this to a Input.GetKeyDown so it triggers once but note that you'll want to up the ball jump force by a ton since it will only trigger on one frame.

    Lastly I'd restructure this a bit since your not supposed to add force in Update. Personally the exception I make to this is for one off forces like a jump. So the final version would look something like this

    EDIT: Updated Version
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallMove : MonoBehaviour
    6. {
    7.     Rigidbody rb;
    8.     public int ballspeed = 10;
    9.     public int jumpspeed = 10;
    10.  
    11.     private float Hmove;
    12.     private float Vmove;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         rb = GetComponent<Rigidbody>();
    18.     }
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         Hmove = Input.GetAxis("Horizontal");
    23.         Vmove = Input.GetAxis("Vertical");
    24.    
    25.         if (Input.GetKeyDown(KeyCode.Space))
    26.         {
    27.             Vector3 balljump = new Vector3(0.0f, 100.0f, 0.0f);
    28.             rb.AddForce(balljump * jumpspeed);
    29.         }
    30.     }
    31.  
    32.     void FixedUpdate()
    33.     {
    34.         Vector3 ballmove = new Vector3(Hmove, 0.0f, Vmove);
    35.      
    36.         rb.AddForce(ballmove * ballspeed);
    37.     }
    38.  
    39. }
    40.  
    41.  
    42.  
    Also if you interested in learning more about C# i'd recommend this tutorial. Once you know the rules of programming things become much easier because you can work things out.


    If you do have a background in programming then I'd look over the unity docs to understand when and how these built in functions are called and how they should be used.
    https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
     
    Last edited: Apr 7, 2020
    Joe-Censored likes this.
  5. itspicklerickandmorty

    itspicklerickandmorty

    Joined:
    Apr 6, 2020
    Posts:
    16
    i did everything you said now the ball just floats up until I stop it and all the code you did it gave gives error CS0103
     
  6. LatchGameDev

    LatchGameDev

    Joined:
    Nov 24, 2014
    Posts:
    61
    Sorry about that. The reason why the code errored out was that Hmove and Vmove were created in the scope of the update function/method. I've moved them above so all of them have access to it.

    As for it just floating up GetKeyDown is what you want since that will trigger once. Not GetKey.

    Updated Version:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallMove : MonoBehaviour
    6. {
    7.     Rigidbody rb;
    8.     public int ballspeed = 10;
    9.     public int jumpspeed = 10;
    10.  
    11.     private float Hmove;
    12.     private float Vmove;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         rb = GetComponent<Rigidbody>();
    18.     }
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         Hmove = Input.GetAxis("Horizontal");
    23.         Vmove = Input.GetAxis("Vertical");
    24.      
    25.         if (Input.GetKeyDown(KeyCode.Space))
    26.         {
    27.             Vector3 balljump = new Vector3(0.0f, 100.0f, 0.0f);
    28.             rb.AddForce(balljump * jumpspeed);
    29.         }
    30.     }
    31.  
    32.     void FixedUpdate()
    33.     {
    34.         Vector3 ballmove = new Vector3(Hmove, 0.0f, Vmove);
    35.        
    36.         rb.AddForce(ballmove * ballspeed);
    37.     }
    38.  
    39. }
    40.  
    41.  
    42.  
     
  7. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I would recommend using Vector3.up * jumpSpeed instead of defining an arbitrary upwards vector like (0,100,0).
    Also, for jumping i would recommend using AddForce(yourForce, ForceMode.Impulse) instead. ForceMode.Impulse is used to apply forces that are supposed to happen more or less instantly, as contrary to happening over time.