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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Question When the character moves left or right for a bit they will randomly stop.

Discussion in 'Scripting' started by I_want_to_create_RPG_GAMES, Mar 19, 2023.

  1. I_want_to_create_RPG_GAMES


    Mar 27, 2020
    The player character will randomly stop and will not be able to move until they jump and I have no idea why. No errors appear. It isn't because of colliders in the game itself. I checked. I'd love if anybody could help me a bit with it and thats Kurt for showing me how to make one of these posts right.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class movement : MonoBehaviour
    6. {
    7.     public PowerCollect script;
    8.     public static int F***;
    9.     public int check=0;
    10.     private Rigidbody2D rb;
    11.     private BoxCollider2D coll;
    12.     private Animator anim;
    13.     private SpriteRenderer sprite;
    14.     private float dirX=0f;
    15.     private enum AnimState{idle, run, jump, fall};
    16.     private AnimState state = AnimState.idle;
    17.     [SerializeField]private AudioSource jumpsoundeffect;
    18.     [SerializeField]private LayerMask jumpableground;
    19.     [SerializeField]private float move=7f;
    20.     [SerializeField]private float jump=14f;
    23.     // Start is called before the first frame update
    24.     private void Start()
    25.     {
    26.         rb=GetComponent<Rigidbody2D>();
    27.         coll=GetComponent<BoxCollider2D>();
    28.         anim=GetComponent<Animator>();
    29.         sprite=GetComponent<SpriteRenderer>();
    31.     }
    33.     // Update is called once per frame
    34.     private void Update()
    35.     {
    36.         F*** = PlayerPrefs.GetInt("F***", 0);
    37.         dirX= Input.GetAxisRaw("Horizontal");
    38.         rb.velocity = new Vector2(dirX * move, rb.velocity.y);
    39.         if (Input.GetButtonDown("Jump") && IsGrounded())
    40.         {
    42.             jumpsoundeffect.Play();
    43.             rb.velocity = new Vector2(rb.velocity.x, jump);
    44.             check=1;
    45.         }
    47.         else if (Input.GetButtonDown("Jump")  && F***>=1 && check==1)
    48.         {
    49.             jumpsoundeffect.Play();
    50.             check=0;
    51.             rb.velocity = new Vector2(rb.velocity.x, jump);
    52.         }
    55.         UpdateAnimationState();
    56.     }
    57.     private void UpdateAnimationState()
    58.     {
    59.         AnimState state;
    60.         if (dirX>0f)
    61.         {
    62.             state =;
    63.             sprite.flipX=false;
    65.         }
    66.         else if (dirX<0f)
    67.         {
    68.             state =;
    69.             sprite.flipX=true;
    70.         }
    71.         else
    72.         {
    73.             state = AnimState.idle;
    74.         }
    76.         if (rb.velocity.y>.1f)
    77.         {
    78.             state = AnimState.jump;
    79.         }
    80.         else if (rb.velocity.y< -.1f)
    81.         {
    82.             state = AnimState.fall;
    83.         }
    84.         anim.SetInteger ("state",(int)state);
    85.     }
    86.     private bool IsGrounded()
    87.     {
    88.         return Physics2D.BoxCast(, coll.bounds.size, 0f, Vector2.down, .1f, jumpableground);
    89.     }
    91. }
    Last edited: Mar 24, 2023 at 5:59 PM
  2. Kurt-Dekker


    Mar 16, 2013
    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    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 order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

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

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: or this answer for Android:

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    When in doubt, print it out!(tm)

    Note: the
    function is an alias for Debug.Log() provided by the MonoBehaviour class.

    After you find out what is going on, if you are still unable to fix it, here is how to report your problem productively in the Unity3D forums:

    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!!!)