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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Player stopped moving

Discussion in 'Community Learning & Teaching' started by artistshc, Mar 30, 2023.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    I'm taking a tutorial and the player stopped being able to move, and I was wondering if anybody could look it over to see what I did wrong. It is from a tutorial that is 7 years old and I don't think the teacher is going to come back and answer my question. Thank you in advance.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     //gonna need a velocity for the player
    8.     public Vector2 velocity;
    9.     //create simple states for input
    10.     private bool walk, walk_left, walk_right, jump;
    11.     //you can put gameobjects on specific layers & with each layer you can specify what layers should be able
    12.     //to collide with other layers so we want to create a layer mask here for our platforms and our walls
    13.     //so that we can say that we want to collide with those
    14.     public LayerMask wallMask;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.        
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         //call CheckPlayerInput() method
    26.         CheckPlayerInput();
    27.         //call UpdatePlayerPosition() method
    28.         UpdatePlayerPosition();
    29.     }
    30.  
    31.     //in order to do stuff with these input values, we need this method
    32.     void UpdatePlayerPosition() {
    33.  
    34.         //player's position
    35.         Vector3 pos = transform.localPosition;
    36.         //if you flip the x scale from 1 to -1 you flip the character to the left - so that is what scale does
    37.         Vector3 scale = transform.localScale;
    38.  
    39.         //check to see if the walk boolean is true otherwise we don't want our player to walk at all
    40.         if (walk) {
    41.  
    42.             //then is walk_left true?
    43.             if (walk_left) {
    44.  
    45.                 //then walk to the left
    46.                 pos.x -= velocity.x * Time.deltaTime;
    47.                 //to flip the player left.
    48.                 scale.x = -1;
    49.            
    50.             }
    51.  
    52.             //then is walk_right true?
    53.             if (walk_right)
    54.             {
    55.                 print("right");
    56.                 //then walk to the right
    57.                 pos.x += velocity.x * Time.deltaTime;
    58.                 //the player will be facing right
    59.                 scale.x = 1;
    60.  
    61.             }
    62.             //call the checkwallsrays method and pass inthe position and scale
    63.             pos = CheckWallRays(pos, scale.x);
    64.  
    65.             //take the position that we just assessed and give it to the transform.localposition to update it
    66.             transform.localPosition = pos;
    67.             //same with scale
    68.             transform.localScale = scale;
    69.         }
    70.    
    71.    
    72.     }
    73.  
    74.     void CheckPlayerInput() {
    75.  
    76.         //if left arrow key pressed
    77.         bool input_left = Input.GetKey(KeyCode.LeftArrow);
    78.         //if right arrow key pressed
    79.         bool input_right = Input.GetKey(KeyCode.RightArrow);
    80.         //if spacebar (jump)  pressed
    81.         bool input_space = Input.GetKeyDown(KeyCode.Space);
    82.         //checks to see if input_left is true or input_right is true then walk is true
    83.         walk = input_left || input_right;
    84.         //checks to see if input_left is true AND input_right is NOT true then walk_left is true
    85.         walk_left = input_left && !input_right;
    86.         //checks to see if input_left is NOT true AND input_right IS true then walk_right is true
    87.         walk_right = !input_left && input_right;
    88.         //jump is only true if the space bar is down
    89.         jump = input_space;
    90.  
    91.     }
    92.  
    93.  
    94.     //gonna pass in the Vector3 position and direction of the player
    95.     Vector3 CheckWallRays(Vector3 pos, float direction) {
    96.  
    97.         //depending on the direction of the player is facing we are gonna have to do raycasting
    98.         //either to the left or the right
    99.         //to keep things simple we are going to origin points for our rays
    100.         //we are creating 3 rays from the player (one at his feet, one at his head, and one at his midsection)
    101.         //lets create for the positions for those rays
    102.         // this is origin position for the top of the player
    103.         // the pos x + direction * .4f is a value - taking x position of player which is center of player
    104.         //and add direction which will either be 1 or -1 plus .4f is gonna equal where the ray begins at on top of player
    105.         Vector2 originTop = new Vector2(pos.x + direction * .4f, pos.y + 1f - 0.2f);
    106.         Vector2 originMiddle = new Vector2(pos.x + direction * .4f, pos.y);
    107.         Vector2 originBottom = new Vector2(pos.x + direction * .4f, pos.y - 1f - 0.2f);
    108.  
    109.         //To create a raycast we use the Physics2d class
    110.         RaycastHit2D wallTop = Physics2D.Raycast(originTop, new Vector2(direction, 0), velocity.x * Time.deltaTime, wallMask);
    111.         RaycastHit2D wallMiddle = Physics2D.Raycast(originMiddle, new Vector2(direction, 0), velocity.x * Time.deltaTime, wallMask);
    112.         RaycastHit2D wallBottom = Physics2D.Raycast(originBottom, new Vector2(direction, 0), velocity.x * Time.deltaTime, wallMask);
    113.  
    114.         //Now we want to check each of our hit objects to see if they actually contain a collider
    115.         if (wallTop.collider != null || wallMiddle.collider != null || wallBottom.collider != null)
    116.             //then there was a collider  .. if our player collides with anything, we don't want our player to be able to move into it
    117.             //we need to decrement the x position by the position the player was before he hit into the collider
    118.             //and multiply that bit the direction
    119.             pos.x -= velocity.x * Time.deltaTime * direction;
    120.  
    121.         //return the position
    122.         return pos;
    123.     }
    124.  
    125. }
    126.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    If it worked then something changed and made it stop working.

    Remember the code is only a teeny-tiny itsy-bitsy part of the problem. Everything set up in scenes and prefabs and assets are equally or even more important.

    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
    Debug.Log()
    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 names of the GameObjects or Components involved?
    - 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
    Debug.Log("Problem!",this);


    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: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    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:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

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

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Thank you so much for your help. :D
     
  4. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    So I figured out the problem is

    pos = CheckWallRays(pos, scale.x);

    on line 63.

    However, this is the same exact thing the teacher put in his code. I see in the comments, that a lot of people have the same thing wrong in their code (that the player stopped moving). Do you have any idea what is wrong with that line. I have never used CheckWallRays before. This lesson was on Unity5, I don't know if that is the reason.