Weird Player Movement Issue!

,

Good day/night! I’m making a 2D shooter and I’ve come across a strange error in the game that appeared yesterday. The player movement was working before, but then it suddenly stopped. I redid the movement code and that didn’t work. I used a few debug statements to see if Update() was reaching the code and it was. After a few hours of testing new things, I realized something. The player isn’t moving, but something is.

I have a segment of code that makes the enemy shoot a bullet at the player. No matter the position of the player, the bullet will head towards it. When I hit the arrow keys, the player was still, but the enemy’s bullets would move in the direction I “moved” to. Nothing else has the player movement script.

Here is my current player movement class. If you see that I have two areas that deal with movement, that was me trying to fix the issue. One is commented out, feel free to test the other one since neither of them work:

I commented out a few variables since to see if they were part of the problem. Disregard them.

public class PlayerMovement : MonoBehaviour
{
    public GameObject bullets; //bullet prefab
    public GameObject bulletPos1;
    public float speed;
    public int playerLives;
    //private CameraShake shake;

    // Start is called before the first frame update
    void Start()
    {
        playerLives = 3;
     //   shake = GameObject.FindGameObjectWithTag("Screenshake").GetComponent<CameraShake>();
        //hitByEnemy = false;

    }

    // Update is called once per frame
    void Update()
    {
       
        /* 2nd movement code, not working
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            Debug.Log("Pressing down");
            Vector2 pos = transform.position;
            pos.x-=speed;
            transform.position = pos;

        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            Debug.Log("Pressing down2");
            Vector2 pos = transform.position;
            pos.x += speed;
            transform.position = pos;

        }
      
        if (Input.GetKey(KeyCode.UpArrow))
        {
            Debug.Log("Pressing down3");
            Vector2 pos = transform.position;
            pos.y += speed;
            transform.position = pos;
        }
      
        if (Input.GetKey(KeyCode.DownArrow))
        {
            Debug.Log("Pressing down4");
            Vector2 pos = transform.position;
            pos.y -= speed;
            transform.position = pos;

        } */

        //fire bullets when spacebar is hit, works
        if (Input.GetKeyDown("space"))
        {
            GameObject bullet1 = (GameObject)Instantiate(bullets);
            bullet1.transform.position = bulletPos1.transform.position;
        }
       
       //For the first movement code
       float x = Input.GetAxisRaw("Horizontal");
       float y = Input.GetAxisRaw("Vertical");

        Vector2 direction = new Vector2(x, y).normalized;

        Move(direction);
    }

//first movement code
    void Move(Vector2 direction)
    {

        Debug.Log("In the move func");
        Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); //bottom left of cam
        Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); //top right of cam

        max.x = max.x - 0.225f;
        min.x = min.x + 0.225f;

        max.y = max.y - 0.285f;
        max.y = max.y + 0.285f;

        Vector2 pos = transform.position;
        pos += direction * speed * Time.deltaTime;

        pos.x = Mathf.Clamp(pos.x, min.x, max.x);
        pos.y = Mathf.Clamp(pos.y, min.y, max.y);

        transform.position = pos;
    }

//works
    void OnTriggerEnter2D(Collider2D col)
    {
        Debug.Log("Lives: " + playerLives);
        if ((col.tag == "EnemyShip") || (col.tag == "EnemyBullet"))
        {
            if (playerLives > 0)
            {
                //shake.CamShake();
                playerLives -= 1;
            }
            else
            {
               // shake.CamShake();
                Destroy(gameObject);
            }
           
        }
    }
}

All possible solutions are appreciated. If you need me to upload anything else, just ask. Thank you!

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

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

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 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 put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

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.

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: