Search Unity

problems with jumping code

Discussion in 'Scripting' started by gladosthedestroyer, May 10, 2018.

  1. gladosthedestroyer

    gladosthedestroyer

    Joined:
    May 9, 2018
    Posts:
    11
    hi I'm a college student trying to learn unity and I have come across an issue where the character controls are not linked with the player despite all the code being correct (from a YouTube tutorial).


    Here's the error:


    UnassignedReferenceException: The variable groundCheckPoint of PlayerController has not been assigned.
    You probably need to assign the groundCheckPoint variable of the PlayerController script in the inspector.
    UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/TransformBindings.gen.cs:27)
    PlayerController.Update () (at Assets/Assets/scripts/PlayerController.cs:34)

    Here's most of the code.

    upload_2018-5-10_10-18-13.png
    If any ideas please let me know

    screenshot of the game view.

    upload_2018-5-10_10-22-40.png
     

    Attached Files:

  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    did you put something in that reference slot in the inspector? ... o_O


    also, code tags rather than a screen shot of code please :) https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. gladosthedestroyer

    gladosthedestroyer

    Joined:
    May 9, 2018
    Posts:
    11
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class PlayerController : MonoBehaviour
    {
    public float moveSpeed;
    public float jumpforce;
    public KeyCode left;
    public KeyCode right;
    public KeyCode jump;
    public KeyCode throwBall;
    private Rigidbody2D theRB;
    public Transform groundCheckPoint;
    public float groundCheckRadius;
    public LayerMask whatisGround;
    public bool isGrounded;
    // Use this for initialization
    void Start() {
    theRB = GetComponent<Rigidbody2D>();
    }
    // Update is called once per frame
    void Update() {

    {
    isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatisGround);
    if (Input.GetKey(left))
    {
    theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
    }
    else if (Input.GetKey(right))
    {
    theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
    }
    else {
    theRB.velocity = new Vector2(0, theRB.velocity.y);
    }
    if (Input.GetKeyDown(jump) && isGrounded)
    {
    theRB.velocity = new Vector2(theRB.velocity.x, jumpforce);
    }
    }
    }
    }
     
  4. gladosthedestroyer

    gladosthedestroyer

    Joined:
    May 9, 2018
    Posts:
    11
    no i did not put anything in the inspector im not fully aware of unity requirements so im mostly new to this should there be something in the inspector?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Drag and drop the Transform 'groundCheckPoint' into that variable slot in the inspector (from the hiearchy). :)
     
  6. gladosthedestroyer

    gladosthedestroyer

    Joined:
    May 9, 2018
    Posts:
    11
    thanks very much that fixed the problem
     
  7. gladosthedestroyer

    gladosthedestroyer

    Joined:
    May 9, 2018
    Posts:
    11
    having just tested my program i now have another problem as the function to check if the player is touching the ground while jumping to prevent from jumping while in mid air the player no longer will jump as the player starts on the ground the function to prevent player from jumping apply when the game begins which prevents you from jumping at any point any suggestions to fix this
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Are you saying that the character is not considered 'grounded' when the game starts?
    If that's the case, you should Debug that to find out why.

    Also, you might find it a good change to move your code to fixed update, except for the jump button; the jump button can be detected in Update and consumed in FixedUpdate.
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Here is some code I wrote for someone else recently that I hadn't yet deleted. One difference is the ground detection, which you could change to be more like yours, instead, and the rest would be the same/very similar:
    Code (csharp):
    1. public class Test11 : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     Rigidbody2D rb;
    5.     bool jump = false;
    6.     float speed = 2;
    7.     float jumpHeight = 3;
    8.     float playerBottom;
    9.     bool grounded = false;
    10.     void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody2D>();
    13.         playerBottom = GetComponent<Collider2D>().bounds.extents.y;
    14.     }
    15.     void Update()
    16.     {
    17.         if (Input.GetKeyDown(KeyCode.Space) && grounded) jump = true;
    18.     }
    19.     void FixedUpdate()
    20.     {
    21.         grounded = false;
    22.         RaycastHit2D hit = Physics2D.Raycast(transform.position - new Vector3(0, playerBottom + .01f, 0), Vector3.down, .05f);
    23.         Debug.DrawRay(transform.position - new Vector3(0, playerBottom, 0), Vector3.down * .05f,Color.red);
    24.         float h = Input.GetAxisRaw("Horizontal");
    25.  
    26.         if (hit) // on the ground
    27.         {
    28.             print("Hit: " + hit.collider.name);
    29.             grounded = true;
    30.             rb.velocity = new Vector2(h * speed, rb.velocity.y);
    31.             if (jump)
    32.             {
    33.                 rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
    34.                 jump = false;
    35.             }
    36.         }
    37.     }
    38. }
     
  10. gladosthedestroyer

    gladosthedestroyer

    Joined:
    May 9, 2018
    Posts:
    11
    i have uploaded a video on YouTube showing the problem i have iv tested your code as well which is also on the video once you see the issue you might get it
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm, well did you ever look in the scene to see the debug draw ray and where it is with my code?
    It might require some tweaking. When I tested the code it didn't allow for jumping while in the air.

    Your code doesn't seem to detect the ground at all.

    If you want, you could post a unity package here with that 1 level, and I could try to take a look at it for ya.
     
  12. gladosthedestroyer

    gladosthedestroyer

    Joined:
    May 9, 2018
    Posts:
    11
    Hi - Just a thought - If I wanted to stop it jumping in mid air, I thought it might work if I put grounded = false inside an else statement that corresponds to if (hit). This didn't work though, so I guess its not that simple. Am I on the right lines with this idea, or does it need more complex coding??

    Another thing I thought might be affecting it, is the values I have set in the inspector. Do I need to set anything specific for this to work.

    Finally - Sorry to appear foolish, I'm not sure what a unity package is and how to upload it. If you could help, that would be great!
     
    Last edited: May 18, 2018
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    A unity package is something you can export from the editor. In the assets menu, you'll see an option "Export package".

    The code I posted should prevent jumping in the air, since it should only allow you to jump if the raycast hits something. It's possible that the raycast is maybe hitting something, just not the ground, and that's causing a problem.
    Whether you use the ground check (Overlap circle) or the raycast, basically you don't want to allow jumping if there's no 'hit' ..

    In the version with my code, what does the print statement output to the console when you jump while in the air? What does it say it's hitting? That might help you understand what's happening, so you can fix it.
     
  14. gladosthedestroyer

    gladosthedestroyer

    Joined:
    May 9, 2018
    Posts:
    11
    Hit: player 1
    UnityEngine.MonoBehaviour:print(Object)
    PlayerController:FixedUpdate() (at Assets/Assets/scripts/PlayerController.cs:34)



    this comes up after you jump

    Hit: SnowBallFightArt_28 (9)
    UnityEngine.MonoBehaviour:print(Object)
    PlayerController:FixedUpdate() (at Assets/Assets/scripts/PlayerController.cs:34)

    also it comes up saying the package is too large to upload any pacific method?
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay so you are hitting yourself in the first example, and then snow ball fight art (whatever that is) while you're in the air.
    So, if you use the overlap for ground detection, you should be checking for the ground layer, probably.
    Alternatively, you can adjust the raycast so it won't hit the player, at least. I'm not sure what the snow ball art is, though.
     
  16. gladosthedestroyer

    gladosthedestroyer

    Joined:
    May 9, 2018
    Posts:
    11
    sorry about my basic level of knowledge but where to i find that function or how is it something to add onto the code or a button to select on the unity inspector
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I can try to explain. Instead of using the Raycast from my code, you can use the line from the code you posted:
    Code (csharp):
    1. isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatisGround);
    Just make sure you have setup the 'whatisGround' layermask properly. You have to set the layer of your ground objects to a 'ground' layer. Then make sure the mask is set to the same layer.

    Just swap out the code where it says if (hit) so it says if (grounded).

    I hope that helps.

    If this is for school, do you learn this stuff in school? Usually the best advice for people starting out is to go over some basic Unity tutorials. :)
     
  18. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Just out of curiosity, is there a reason that you are using the physics engine for your character? I very much prefer to use the CharacterController base class, as physics tend to make crappy controls.

    CharacterController has the convenient boolean "isGrounded." This by itself is pretty useless because it seems unreliable from frame to frame, but it's very reliable over the course of a few frames. The way I check if a player is "on the ground" is by checking if the time since the last isGrounded is less than a fraction of a second. This provides the additional benefit of what's called "coyote time" which is where video game characters are allowed to jump just slightly after walking off of the edge.
     
  19. gladosthedestroyer

    gladosthedestroyer

    Joined:
    May 9, 2018
    Posts:
    11
    no i am at college doing a computing course and for my final project unit 8 i desided to create a game as i primary aim to be a game developer and so on so on so iv been researching and more less teaching myself with the added coding skills i already have its been currently going well but this problem has obviously put me to a hult at the moment
     
  20. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Did you try what I said about the layermask and the ground check stuff in my last post?
     
  21. gladosthedestroyer

    gladosthedestroyer

    Joined:
    May 9, 2018
    Posts:
    11
    yes it had no effect on it but no worrys i will be visiting a unity developer at a university who may be able to resolve it at the moment iv moved on to animations as the bug inst a mager issue that prevents the program from working