Forum begins after the advertisement:


[Part 9] Player doesn’t move at all

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 9] Player doesn’t move at all

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #13543
    DeviD DallaS (DPnikman)
    Level 12
    Participant
    Helpful?
    Up
    0
    ::

    Stopped and got tilted on Part 9. After I’ve done Display result screen information, just wanted to check all in Editor. Started the game, choose a character, and all I see is my character does not move, has no animation on turning to different sides, he got hit and moved by bats!

    PlayerMovement Script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerMovement : MonoBehaviour
    {
        //Movement
        public float moveSpeed;
        [HideInInspector]
        public float lastHorizontalVector;
        [HideInInspector]
        public float lastVerticalVector;
        [HideInInspector]
        public Vector2 moveDir;
        [HideInInspector]
        public Vector2 lastMovedVector;
    
        //References
        Rigidbody2D rb;
        PlayerStats player;
    
        void Start()
        {
            player = GetComponent<PlayerStats>();
            rb = GetComponent<Rigidbody2D>();
            lastMovedVector = new Vector2(1, 0f); //If we don't do this and game starts up and player doesn't move, the projectile weapon will no momentum
            Debug.Log("PlayerMovement script is started.");
    
        }
    
        void Update()
        {
            InputManagement();
    
        }
    
        void FixedUpdate()
        {
            Move();
    
        }
        void InputManagement()
        {
            if(GameManager.instance.isGameOver)
            {
                return;
            }
    
            float moveX = Input.GetAxisRaw("Horizontal");
            float moveY = Input.GetAxisRaw("Vertical");
    
            Debug.Log("Move X: " + moveX);
            Debug.Log("Move Y: " + moveY);
    
            moveDir = new Vector2(moveX, moveY).normalized;
    
            Debug.Log("Normalized Move Direction: " + moveDir);
    
            if (moveDir.x != 0)
            {
                lastHorizontalVector = moveDir.x;
                lastMovedVector = new Vector2(lastHorizontalVector, 0f); // Last moved X
            }
    
            if (moveDir.y != 0) 
            { 
            lastVerticalVector = moveDir.y;
                lastMovedVector = new Vector2(0f, lastVerticalVector); //Last moved Y
            }
    
            if(moveDir.x != 0 && moveDir.y != 0)
            {
                lastMovedVector = new Vector2(lastHorizontalVector, lastVerticalVector); //While moving
            }
        }
    
        void Move()
        {
            if (GameManager.instance.isGameOver)
            {
                return;
            }
    
            Vector2 velocity = new Vector2(moveDir.x * player.CurrentMoveSpeed, moveDir.y * player.CurrentMoveSpeed);
            rb.velocity = velocity;
    
            Debug.Log("Rigidbody Velocity: " + velocity);
            Debug.Log("Position: " + transform.position);
        }
    
    }

    RigidBody and Collider params screenshot:

    View post on imgur.com
    #13544
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Hi Devid, are there any errors in the Console?

    I notice also that you are debugging the move X and move Y values:

    Debug.Log("Move X: " + moveX);
    Debug.Log("Move Y: " + moveY);

    Do the values change when you press the movement keys (even though the player doesn’t move)?

    #13548
    DeviD DallaS (DPnikman)
    Level 12
    Participant
    Helpful?
    Up
    0
    ::

    Nope, there are no error’s. I added debugs because tried to find help from chatGPT, and he suggested me use this couple of debug lines to see if is there any reaction when I press input keys. But the only message I see is related to Debug.Log(“PlayerMovement script is started.”);

    Also I’ve tried to check if I ran into some key input problem in Project Settings, but as I remember it was correct.

    View post on imgur.com
    #13550
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    I suspect that the isGameOver attribute is checked on your GameManager. This is why the input is not reading, because the only way the code doesn’t run is if the highlighted section here returns true:

        void InputManagement()
        {
            if(GameManager.instance.isGameOver)
            {
                return;
            }
    
            float moveX = Input.GetAxisRaw("Horizontal");
            float moveY = Input.GetAxisRaw("Vertical");
    
            Debug.Log("Move X: " + moveX);
            Debug.Log("Move Y: " + moveY);
    
            moveDir = new Vector2(moveX, moveY).normalized;
    
            Debug.Log("Normalized Move Direction: " + moveDir);
    
            if (moveDir.x != 0)
            {
                lastHorizontalVector = moveDir.x;
                lastMovedVector = new Vector2(lastHorizontalVector, 0f); // Last moved X
            }
    
            if (moveDir.y != 0) 
            { 
            lastVerticalVector = moveDir.y;
                lastMovedVector = new Vector2(0f, lastVerticalVector); //Last moved Y
            }
    
            if(moveDir.x != 0 && moveDir.y != 0)
            {
                lastMovedVector = new Vector2(lastHorizontalVector, lastVerticalVector); //While moving
            }
        }
    

    Can you check your GameManager component to see if isGameOver is checked?

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: