# Player stopped moving

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.

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
//gonna need a velocity for the player
public Vector2 velocity;
//create simple states for input
private bool walk, walk_left, walk_right, jump;
//you can put gameobjects on specific layers & with each layer you can specify what layers should be able
//to collide with other layers so we want to create a layer mask here for our platforms and our walls
//so that we can say that we want to collide with those

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
//call CheckPlayerInput() method
CheckPlayerInput();
//call UpdatePlayerPosition() method
UpdatePlayerPosition();
}

//in order to do stuff with these input values, we need this method
void UpdatePlayerPosition() {

//player's position
Vector3 pos = transform.localPosition;
//if you flip the x scale from 1 to -1 you flip the character to the left - so that is what scale does
Vector3 scale = transform.localScale;

//check to see if the walk boolean is true otherwise we don't want our player to walk at all
if (walk) {

//then is walk_left true?
if (walk_left) {

//then walk to the left
pos.x -= velocity.x * Time.deltaTime;
//to flip the player left.
scale.x = -1;

}

//then is walk_right true?
if (walk_right)
{
print("right");
//then walk to the right
pos.x += velocity.x * Time.deltaTime;
//the player will be facing right
scale.x = 1;

}
//call the checkwallsrays method and pass inthe position and scale
pos = CheckWallRays(pos, scale.x);

//take the position that we just assessed and give it to the transform.localposition to update it
transform.localPosition = pos;
//same with scale
transform.localScale = scale;
}

}

void CheckPlayerInput() {

//if left arrow key pressed
bool input_left = Input.GetKey(KeyCode.LeftArrow);
//if right arrow key pressed
bool input_right = Input.GetKey(KeyCode.RightArrow);
//if spacebar (jump)  pressed
bool input_space = Input.GetKeyDown(KeyCode.Space);
//checks to see if input_left is true or input_right is true then walk is true
walk = input_left || input_right;
//checks to see if input_left is true AND input_right is NOT true then walk_left is true
walk_left = input_left && !input_right;
//checks to see if input_left is NOT true AND input_right IS true then walk_right is true
walk_right = !input_left && input_right;
//jump is only true if the space bar is down
jump = input_space;

}

//gonna pass in the Vector3 position and direction of the player
Vector3 CheckWallRays(Vector3 pos, float direction) {

//depending on the direction of the player is facing we are gonna have to do raycasting
//either to the left or the right
//to keep things simple we are going to origin points for our rays
//we are creating 3 rays from the player (one at his feet, one at his head, and one at his midsection)
//lets create for the positions for those rays
// this is origin position for the top of the player
// the pos x + direction * .4f is a value - taking x position of player which is center of player
//and add direction which will either be 1 or -1 plus .4f is gonna equal where the ray begins at on top of player
Vector2 originTop = new Vector2(pos.x + direction * .4f, pos.y + 1f - 0.2f);
Vector2 originMiddle = new Vector2(pos.x + direction * .4f, pos.y);
Vector2 originBottom = new Vector2(pos.x + direction * .4f, pos.y - 1f - 0.2f);

//To create a raycast we use the Physics2d class
RaycastHit2D wallTop = Physics2D.Raycast(originTop, new Vector2(direction, 0), velocity.x * Time.deltaTime, wallMask);
RaycastHit2D wallMiddle = Physics2D.Raycast(originMiddle, new Vector2(direction, 0), velocity.x * Time.deltaTime, wallMask);
RaycastHit2D wallBottom = Physics2D.Raycast(originBottom, new Vector2(direction, 0), velocity.x * Time.deltaTime, wallMask);

//Now we want to check each of our hit objects to see if they actually contain a collider
if (wallTop.collider != null || wallMiddle.collider != null || wallBottom.collider != null)
//then there was a collider  .. if our player collides with anything, we don't want our player to be able to move into it
//we need to decrement the x position by the position the player was before he hit into the collider
//and multiply that bit the direction
pos.x -= velocity.x * Time.deltaTime * direction;

//return the position
return pos;
}

}
``````

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://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

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://discussions.unity.com/t/839300/3

When in doubt, print it out!™

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

Thank you so much for your help.

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.