Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Bug My Rigid Body is not working in my 2d platformer.

Discussion in '2D' started by G2Trouble, Mar 21, 2023.

  1. G2Trouble

    G2Trouble

    Joined:
    Mar 21, 2023
    Posts:
    2
    I am making a 2d platformer and I have plenty of errors which shouldn't be happening, here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor.Experimental.GraphView;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     private Rigidbody2D rb;
    9.     private SpriteRenderer sprite;
    10.     private Animator anim;
    11.  
    12.     private float dirX = 0;
    13.     [SerializeField] private float moveSpeed = 4f;
    14.     [SerializeField] private float jumpForce = 9f;
    15.     private enum MovementState { idle, running, jumping, falling }
    16.  
    17.     // Start is called before the first frame update
    18.     private void Start()
    19.     {
    20.         rb = GetComponent<Rigidbody2D>();
    21.         sprite = GetComponent<SpriteRenderer>();
    22.         anim = GetComponent<Animator>();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     private void Update()
    27.     {
    28.         dirX = Input.GetAxisRaw("Horizontal");
    29.         rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
    30.  
    31.         if (Input.GetButtonDown("Jump"))
    32.         {
    33.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    34.         }
    35.  
    36.         UpdateAnimationState();
    37.     }
    38.  
    39.     private void UpdateAnimationState()
    40.     {
    41.         MovementState state;
    42.  
    43.         if (dirX > V)
    44.         {
    45.             state = MovementState.running;
    46.             Sprite.flipX = false;
    47.         }
    48.         else if (dirX < 0)
    49.         {
    50.             state = MovementState.running;
    51.             Sprite.flipX = true;
    52.         }
    53.         else
    54.         {
    55.             state = MovementState.idle;
    56.         }
    57.  
    58.         if (rb.velocity.y > .1f)
    59.         {
    60.             state = MovementState.jumping;
    61.         }
    62.         else if (rb.velocity.y < -.1f)
    63.         {
    64.             state = MovementState.falling;
    65.            
    66.         }
    67.         anim.SetInteger("state", (int)state);
    68.     }
    69. }
    70.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,916
    You better get busy fixing them then!

    Since you provided exactly zero information about your errors, here some guesses:

    Here's how:

    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

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  3. G2Trouble

    G2Trouble

    Joined:
    Mar 21, 2023
    Posts:
    2
    Most of the errors are CS0229.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,916
    That's awesome! Get busy up above. You know the drill now.

    The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

    This assumes you have at least written and studied some code and have run into some kind of issue.

    If you haven't even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

    If you just want someone to do it for you, you need go to one of these places:

    https://forum.unity.com/forums/commercial-job-offering.49/

    https://forum.unity.com/forums/non-commercial-collaboration.17/

    https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons

    You may consider trying your luck at ChatGPT. However, if you cannot understand the code ChatGPT makes you, nobody here can either. Remember this is YOUR game, not ours. We have our own games we're working on.

    If you have an ACTUAL problem, eg, not just typing mistakes, here is how to report your problem productively in the Unity3D forums:

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

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)