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

Trying to jump

Discussion in 'Scripting' started by blod, Mar 27, 2016.

  1. blod

    blod

    Joined:
    Mar 24, 2016
    Posts:
    2
    Hi everyone so i am creating my second game with unity and i'm trying to create a jumping mechanic. I want it to be that i can only jump when on the ground so i don't jump off of walls or anything, here's the code i'm using and it doesn't seem to be working. if anyone has any ideas why please respond.bb

    Code (CSharp):
    1.  
    2. private float jumpPowa = 1000f;
    3. private bool grounded = false;
    4. private    Vector3 jumpforce = Vector3.zero;
    5.  
    6. public void applyjump (Vector3 _jumpforce){
    7.         jumpforce = _jumpforce;
    8.  
    9. if (jumpforce != Vector3.zero) {
    10.             rb.AddForce (jumpforce * Time.fixedDeltaTime, ForceMode.Acceleration);
    11.         }
    12.  
    13. void OnCollisionEnter(Collider other) {
    14.         if (other.CompareTag ("ground")) {
    15.             grounded = true;
    16.         }
    17.     }
    18.  
    19.     void OnCollisionExit(Collider other) {
    20.         if (other.CompareTag ("ground")) {
    21.             grounded = false;
    22.         }
    23.     }
    24.  
    25.   Vector3 _jumpforce = Vector3.zero;
    26.         if (Input.GetButton ("Jump")) {
    27.             _jumpforce = Vector3.up * jumpPowa;  
    28.  
    29. if (grounded == true){
    30.         motor.applyjump(_jumpforce);
    31.  
    32.  
     
  2. ILemonDev

    ILemonDev

    Joined:
    Dec 23, 2015
    Posts:
    11
    Was this a direct copy and paste from your code? You seem to have other methods within the applyJumpmethod. This isn't allowed. I removed the OnCollisionExit as there was no need to do a collision exit as you can set grounded to false when you jump. When you enter back on the ground then you set it to true again which is fine.

    Code (CSharp):
    1. private float jumpPowa = 1000f;
    2. private bool grounded = false;
    3. private    Vector3 jumpforce = Vector3.zero;
    4. public void applyjump (Vector3 _jumpforce)
    5. {
    6.       jumpforce = _jumpforce;
    7.       if (jumpforce != Vector3.zero) {
    8.             rb.AddForce (jumpforce * Time.fixedDeltaTime, ForceMode.Acceleration);
    9.         }
    10. }
    11.  
    12. void OnCollisionEnter(Collider other)
    13. {
    14.         if (other.CompareTag ("ground"))
    15.         {
    16.             grounded = true;
    17.         }
    18. }
    19.  
    20. void Update()
    21. {      
    22.         if (Input.GetButton ("Jump"))
    23.         {
    24.             _jumpforce = Vector3.up * jumpPowa;
    25.              if (grounded == true)
    26.             {          
    27.                  applyjump(_jumpforce);
    28.                  grounded = false;
    29.             }
    30.        }  
    31. }
    32.  
     
    blod likes this.
  3. blod

    blod

    Joined:
    Mar 24, 2016
    Posts:
    2
    no it wasn't, my code is split into two scripts a motor and a controller sorry, i didn't want to post a lengthy amount of code. I'm getting

    script error: OnCollisionEnter - This message parameter has to be of type:

    any idea what this means?
     
    Last edited: Mar 27, 2016
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    ground detection with enter and exit functions is unreliable, because the object registers that its off the ground the moment it exits the viable objects.

    So lets say your ground is made up of several objects. The moment you enter the next ground object, you are exiting the previous ground object. so even if the object is visually on the ground, the script dictates it is off the ground since it has exited the previous ground object.

    I don't know how complex your game is going to be, but here's an old method I used which works for simple games. It requires you have a trigger component which is smaller than your main object and poking out of the base of your object.
    Code (CSharp):
    1. private bool onGround;
    2.  
    3. void FixedUpdate(){
    4.     onGround = false;
    5. }
    6. void OnTriggerStay(){
    7.     onGround = true;
    8. }
    This way, you will not have to depend on tags, and the collision is still reliable if your ground is made up of several objects.
     
  5. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    First, I would recommend to linecast down to see if you hit the ground, that is in my opinion far more reliable.
    When you press your jump button, the script may run apply jump multiple times.
    To prevent this just add a boolen called pressJump and on FixedUpdate if pressJump = true, then jump.
    Code (CSharp):
    1. void Update()
    2. {
    3. if(Input.GetKey(Keys.Space))
    4.      pressJump = true;
    5.  
    6. }
    7.  
    8. void  FixedUpdate()
    9. {
    10.  
    11.       if(pressJump == true)
    12.        {
    13.                 ApplyJump(force)
    14.                pressJump = false;
    15.         }
    16.  
    17. }
    Also how you don't get error when your code is not inside a function, are you sure you copy the code correctly?
     
  6. ILemonDev

    ILemonDev

    Joined:
    Dec 23, 2015
    Posts:
    11
    Okay @blod sorry i missed one part within your code. On the OnCollisionEnter and OnCollisionExit functions you need the parameters to be of type Collision instead of collider. Go to this unity documentation to help you http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html

    Code (CSharp):
    1. void OnCollisionEnter(Collision other) {
    2.         if (other.CompareTag ("ground")) {
    3.             grounded = true;
    4.         }
    5.     }
    6.     void OnCollisionExit(Collision other) {
    7.         if (other.CompareTag ("ground")) {
    8.             grounded = false;
    9.         }
    10.     }