Search Unity

<Solved> Unable To Jump. Probably Code Error

Discussion in 'Scripting' started by Jongoji245, Apr 15, 2019.

  1. Jongoji245

    Jongoji245

    Joined:
    Jan 6, 2019
    Posts:
    2
    Hello. So I was following a series of tutorial videos on making a 2D platformer. It is at this point where I've gotten stuck.



    I am able to get attacking and sliding functional with the new structure, but not the jump function

    Here's the code for the player script's new structure that I've written up to this point. In Visual Studio, the Game Object on Line 12 and MyRigidbody.velocity.y == 0 on Line 88 have a yellow line, suggesting a fix is needed. Any thoughts?

    Edit: I am using Unity 2018.3.12f1 and using Visual Studio 2017

    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.  
    4.     private static Player instance;
    5.  
    6.     public static Player Instance
    7.     {
    8.         get
    9.         {
    10.             if (instance == null)
    11.             {
    12.                 instance = GameObject.FindObjectOfType<Player>();
    13.             }
    14.             return instance;
    15.         }
    16.     }
    17.  
    18.     private Animator myAnimator;
    19.  
    20.     [SerializeField]
    21.     private float movementSpeed;
    22.  
    23.     private bool facingRight;
    24.  
    25.     [SerializeField]
    26.     private Transform[] groundPoints;
    27.  
    28.     [SerializeField]
    29.     private float groundRadius;
    30.  
    31.     [SerializeField]
    32.     private LayerMask whatIsGround;
    33.  
    34.     [SerializeField]
    35.     private bool airControl;
    36.  
    37.     [SerializeField]
    38.     private float jumpForce;
    39.  
    40.     public Rigidbody2D MyRigidbody { get; set; }
    41.  
    42.     public bool Attack { get; set; }
    43.  
    44.     public bool Slide { get; set; }
    45.  
    46.     public bool Jump { get; set; }
    47.  
    48.     public bool OnGround { get; set; }
    49.  
    50.     // Start is called before the first frame update
    51.     void Start()
    52.     {
    53.         facingRight = true;
    54.         MyRigidbody = GetComponent<Rigidbody2D>();
    55.         myAnimator = GetComponent<Animator>();
    56.     }
    57.  
    58.     private void Update()
    59.     {
    60.         HandleInput();
    61.     }
    62.  
    63.     // Update is called once per frame
    64.     void FixedUpdate()
    65.     {
    66.         float horizontal = Input.GetAxis("Horizontal");
    67.  
    68.         OnGround = IsGrounded();
    69.  
    70.         HandleMovement(horizontal);
    71.  
    72.         Flip(horizontal);
    73.  
    74.         HandleLayers();
    75.  
    76.     }
    77.  
    78.     private void HandleMovement(float horizontal)
    79.     {
    80.        if (MyRigidbody.velocity.y < 0)
    81.         {
    82.             myAnimator.SetBool("land", true);
    83.         }
    84.        if (!Attack && !Slide && (OnGround || airControl))
    85.         {
    86.             MyRigidbody.velocity = new Vector2(horizontal * movementSpeed, MyRigidbody.velocity.y);
    87.         }
    88.         if (Jump && MyRigidbody.velocity.y == 0)
    89.         {
    90.             MyRigidbody.AddForce(new Vector2(0, jumpForce));
    91.         }
    92.  
    93.         myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
    94.     }
    95.  
    96.     private void HandleInput()
    97.     {
    98.         if (Input.GetKeyDown(KeyCode.Space))
    99.         {
    100.             myAnimator.SetTrigger("jump");
    101.         }
    102.         if (Input.GetKeyDown(KeyCode.LeftShift))
    103.         {
    104.             myAnimator.SetTrigger("attack");
    105.         }
    106.         if (Input.GetKeyDown(KeyCode.LeftControl))
    107.         {
    108.             myAnimator.SetTrigger("slide");
    109.         }
    110.     }
    111.  
    112.     private void Flip(float horizontal)
    113.     {
    114.         if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
    115.         {
    116.             facingRight = !facingRight;
    117.  
    118.             Vector3 theScale = transform.localScale;
    119.  
    120.             theScale.x *= -1;
    121.  
    122.             transform.localScale = theScale;
    123.         }
    124.     }
    125.  
    126.     private bool IsGrounded()
    127.     {
    128.         if (MyRigidbody.velocity.y <= 0)
    129.         {
    130.             foreach (Transform point in groundPoints)
    131.             {
    132.                 Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
    133.  
    134.                 for (int i = 0; i < colliders.Length; i++)
    135.                 {
    136.                     if (colliders[i].gameObject != gameObject)
    137.                     {
    138.                         return true;
    139.                     }
    140.                 }
    141.             }
    142.         }
    143.         return false;
    144.     }
    145.  
    146.     private void HandleLayers()
    147.     {
    148.         if (!OnGround)
    149.         {
    150.             myAnimator.SetLayerWeight(1, 1);
    151.         }
    152.         else
    153.         {
    154.             myAnimator.SetLayerWeight(1, 0);
    155.         }
    156.     }
    157.  
    158. }
    159.  
     
    Last edited: Apr 15, 2019
  2. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    any error or he just dont jump?
    Also you having jumpforce serialized , dont you have set it to 0 in inspector?
     
  3. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    There doesn't seem to be anywhere in the code that sets 'Jump' to true - is that done in another component?

    Also, it's unlikely that MyRigidbody.velocity.y will ever equal 0 exactly as it's floating point. It may end up being 0.00001 which would prevent it jumping. Probably best to change that to something like this:

    Code (CSharp):
    1. if( Jump && Mathf.Approximately( MyRigidbody.velocity.y , 0.0f ) )
     
  4. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    Are you sure you don't rush? maybe his jumps dont works too yet.
    because on 22:00 minutes of this vid you posted, he sets it to true in a diff script
     
  5. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    @Reedex Yeah, you're right - I hadn't looked at the video as I thought the code was modifications to the original tutorial
     
  6. Jongoji245

    Jongoji245

    Joined:
    Jan 6, 2019
    Posts:
    2
    Sorry for the late response. Yeah, the JumpForce was set to zero. That and I may have forgotten to capitalize a few names.

    Case solved. Thank you all for your help.