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

Question Unity 2021.3.19f1 Can't jump

Discussion in '2D' started by KeciAdam2, Mar 10, 2023.

  1. KeciAdam2

    KeciAdam2

    Joined:
    Mar 2, 2023
    Posts:
    3
    So i cant jump it says: <Missing PlayerController.OnJump>

    Player Controller:


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor.Rendering.LookDev;
    5. using UnityEngine;
    6. using UnityEngine.InputSystem;
    7.  
    8. [RequireComponent(typeof(Rigidbody2D), typeof(TouchingDirections))]
    9.  
    10. public class PlayerController : MonoBehaviour
    11. {
    12.     public float walkSpeed = 5f;
    13.     public float runSpeed = 8f;
    14.     public float jumpImpulse = 10f;
    15.     Vector2 moveInput;
    16.     TouchingDirections touchingDirections;
    17.  
    18.     public float CurrentMoveSpeed { get
    19.         {
    20.             if(IsMoving)
    21.             {
    22.                 if (IsRunning)
    23.                 {
    24.                     return runSpeed;
    25.                 } else
    26.                 {
    27.                     return walkSpeed;
    28.                 }
    29.             } else
    30.             {
    31.                 //Idle speed is 0
    32.                 return 0;
    33.             }
    34.         } }
    35.  
    36.     [SerializeField]
    37.     private bool _isMoving = false;
    38.  
    39.     public bool IsMoving { get
    40.         {
    41.             return _isMoving;      
    42.         }
    43.         private set
    44.         {
    45.             _isMoving = value;
    46.             animator.SetBool(AnimationStrings.isMoving, value);
    47.         }
    48.     }
    49.  
    50.     [SerializeField]
    51.     private bool _isRunning = false;
    52.  
    53.     public bool IsRunning
    54.     {
    55.         get
    56.         {
    57.             return _isRunning;
    58.         }
    59.         set
    60.         {
    61.             _isRunning = value;
    62.             animator.SetBool(AnimationStrings.isRunning, value);
    63.         }
    64.     }
    65.  
    66.     public bool _isFacingRight = true;
    67.  
    68.     public bool IsFacingRight { get { return _isFacingRight; } private set {
    69.             if (_isFacingRight != value)
    70.             {
    71.                 // Flip the local scale to make the player face the opposite direction
    72.                 transform.localScale *= new Vector2(-1, 1);
    73.             }
    74.  
    75.             _isFacingRight = value;
    76.        
    77.         } }
    78.  
    79.     Rigidbody2D rb;
    80.     Animator animator;
    81.  
    82.     private void Awake()
    83.     {
    84.         rb = GetComponent<Rigidbody2D>();
    85.         animator = GetComponent<Animator>();
    86.         touchingDirections = GetComponent<TouchingDirections>();
    87.     }
    88.  
    89.  
    90.     private void FixedUpdate()
    91.     {
    92.         rb.velocity = new Vector2(moveInput.x * CurrentMoveSpeed, rb.velocity.y);
    93.  
    94.         animator.SetFloat(AnimationStrings.yVelocity, rb.velocity.y);
    95.     }
    96.  
    97.     public void OnMove(InputAction.CallbackContext context)
    98.     {
    99.         moveInput = context.ReadValue<Vector2>();
    100.  
    101.         IsMoving = moveInput != Vector2.zero;
    102.  
    103.         SetFacingDirection(moveInput);
    104.     }
    105.  
    106.     private void SetFacingDirection(Vector2 moveInput)
    107.     {
    108.         if(moveInput.x > 0 && !IsFacingRight)
    109.         {
    110.             // Face the right
    111.             IsFacingRight = true;
    112.         }
    113.         else if (moveInput.x < 0 && IsFacingRight)
    114.         {
    115.             // Face the left
    116.             IsFacingRight= false;
    117.         }
    118.     }
    119.  
    120.     public void OnRun(InputAction.CallbackContext context)
    121.     {
    122.         if (context.started)
    123.         {
    124.             IsRunning = true;
    125.         } else if(context.canceled)
    126.         {
    127.             IsRunning = false;
    128.         }
    129.     }
    130.  
    131.     public void OnJump(InputAction.CallbackContext context)
    132.     {
    133.         // TODO Check if alive as well
    134.         if (context.started && touchingDirections.IsGrounded)
    135.         {
    136.             animator.SetTrigger(AnimationStrings.jump);
    137.             rb.velocity = new Vector2(rb.velocity.x, jumpImpulse);
    138.         }
    139.     }
    140. }


    AnimationStrings:


    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6.  
    7. internal class AnimationStrings
    8. {
    9.     internal static string isMoving = "isMoving";
    10.     internal static string isRunning = "isRunning";
    11.     internal static string isGrounded = "isGrounded";
    12.     internal static string yVelocity = "yVelocity";
    13.     internal static string jump = "jump";
    14. }  


    TouchingDirections:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TouchingDirections : MonoBehaviour
    6. {
    7.     public ContactFilter2D castFilter;
    8.     public float groundDistance = 0.05f;
    9.  
    10.     CapsuleCollider2D touchingCol;
    11.     Animator animator;
    12.  
    13.     RaycastHit2D[] groundHits = new RaycastHit2D[5];
    14.  
    15.     [SerializeField]
    16.     private bool _isGrounded;
    17.  
    18.     public bool IsGrounded { get {
    19.             return _isGrounded;
    20.         } private set {
    21.             _isGrounded = value;
    22.             animator.SetBool(AnimationStrings.isGrounded, value);
    23.  
    24.         } }
    25.  
    26.     private void Awake()
    27.     {
    28.         touchingCol = GetComponent<CapsuleCollider2D>();
    29.         animator = GetComponent<Animator>();
    30.     }
    31.  
    32.     void FixedUpdate()
    33.     {
    34.         IsGrounded = touchingCol.Cast(Vector2.down, castFilter, groundHits, groundDistance) > 0;
    35.     }
    36. }
    37.  
     
  2. KeciAdam2

    KeciAdam2

    Joined:
    Mar 2, 2023
    Posts:
    3
    No errors btw
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    No, this is the error:

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
  4. KeciAdam2

    KeciAdam2

    Joined:
    Mar 2, 2023
    Posts:
    3
    is there any errors in my script im new to unity
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    It is critical that you follow ALL steps of a tutorial.

    Code is a tiny part of the problem.

    It appears you did not set up the OnFoot callback that the script requires so you're getting the missing error.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!