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
- This topic has 3 replies, 2 voices, and was last updated 8 months ago by Terence.
-
AuthorPosts
-
March 13, 2024 at 5:30 am #13543::
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
(); rb = GetComponent (); 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:
March 13, 2024 at 6:36 am #13544::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)?
March 13, 2024 at 8:24 am #13548::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.
March 13, 2024 at 6:52 pm #13550::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? -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: