Search Unity

Help I got this error ! i was whatching one of Brackys vids

Discussion in 'Scripting' started by MrBlub2010, Sep 27, 2020.

  1. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    My Jump code just wont seem to work. I need help!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MovementSci : MonoBehaviour
    6. {
    7.  
    8.     public CharacterController controller;
    9.  
    10.     public float speed = 12f;
    11.     public float gravity = -9.81f;
    12.     public float jumpheight = 3f;
    13.  
    14.     public Transform groundCheck;
    15.     public float groundDistance = 0.4f;
    16.     public LayerMask groundMask;
    17.  
    18.     Vector3 velocity;
    19.     bool isGrounded;
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    25.  
    26.         if(isGrounded && velocity.y < 0)
    27.         {
    28.             velocity.y = -2f;
    29.         }
    30.  
    31.         float x = Input.GetAxis("Horizontal");
    32.         float z = Input.GetAxis("Vertical");
    33.  
    34.         Vector3 move = transform.right * x + transform.forward * z;
    35.  
    36.         controller.Move(move * speed * Time.deltaTime);
    37.  
    38.         if(Input.GetButtonDown("Jump") && isGrounded)
    39.         {
    40.             velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
    41.         }
    42.  
    43.         velocity.y += gravity * Time.deltaTime;
    44.  
    45.         controller.Move(velocity * Time.deltaTime);
    46.        
    47.        }
    48. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    "I got this error" is completely useless. I get errors all day long in fact!

    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220

    Help us to help you.
     
  3. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    @Kurt-Dekker Wasnt meant to type "i got this error." Its just not jumping
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Excellent! We're getting somewhere, now I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    MrBlub2010 likes this.
  5. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
  6. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    Where Should i put my debug.log? and what other code do i need to put on it to make it tell me its jumping
    ?
     
  7. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    You should put a debug.log into the "if(Input.GetButtonDown("Jump") && isGrounded)", to see if button press is triggered. If you don´t see the log in the console, you know, that nothing happens if you press the jump button.

    If that works, i would maybe check if the "isGrounded" field has the value which you expect.
     
  8. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    @Sphinks Im not sure how im ment to do that?
     
  9. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    In the if statement, just do

    if(Input.GetButtonDown("Jump") && isGrounded)
    {
    Debug.Log("Jump button pressed");
    velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
    }


    If you run the game in unity, check if you see the output "Jump button pressed" in the console.

    To see, if "isGrounded" is true (what is has to be, to enter your if statements) you can put a

    Debug.Log(isGrounded);


    under the line 24.
    You can do such logs to check each value you need or you use the debug function of visual studio to debug the code (if you use VS).
     
    Last edited: Sep 28, 2020
  10. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    Yes I use Visual Studio Code. And now i have added the Debug.log statement when i press space (Jump) It doesnt come up in console with anyting? @Sphinks
     
  11. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    Ok, so it never enters that if statement. Put the other log to see if "isGrounded" is true.

    Just to be sure, did you add the script to your player (object) in the scene ?
     
  12. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    Ah It is false! Its saying false, That Debug.Log(isGrounded); actually is telling me if it is false or not.
     
  13. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    How do i make it grounded tho? the player is a capsule.
     
  14. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    OK, so you have to look, why it is false. I don´t know what to do to solve that problem, sry. Is 0.4 the correct value for "groundDistance" ?

    But you said in the title, you have this from a tutorial video. So I would check the video again and try to figure out, if you any step/configuration missed, especially if something is done in the scene.
     
  15. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    check the tutorial and see how you have to setup the ground object, your groundmask is not registering the collision between the player and the ground
     
  16. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    hmm ok ill have a look
     
  17. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    Im not sure guys. I really need to get this to work :(
     
  18. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    But we can´t do more. There is anything wrong in "Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);" or you didn´t add the script to the player.

    Check the objects for groundCheck and groundMask as @raarc already said and check if the position of your player is correct.

    Can you post the link of the video maybe we can find something, but it looks like there is something missing/not correct in the inspector
     
    Last edited: Sep 28, 2020
    raarc likes this.
  19. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    Got it working :)