Forum begins after the advertisement:
[Part 1] Turning left and right
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 1] Turning left and right
- This topic has 8 replies, 3 voices, and was last updated 1 year ago by Terence.
-
AuthorPosts
-
October 26, 2023 at 1:32 am #12116::
I have problem, dont know what can be problem here, so i will just put code and error here, hopefully you can help me.
This is PlayerMovement.cs:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { // Pohyb public float moveSpeed; Rigidbody2D rb; [HideInInspector] public Vector2 moveDir; //na to aby som vedel, kde sa pozera void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() // frame rate dependent { InputManagement(); } void FixedUpdate() // frame rate independent { Move(); } void InputManagement() { float moveX = Input.GetAxisRaw("Horizontal"); //reakcia na to co sme stlacili float moveY = Input.GetAxisRaw("Vertical"); //reakcia na to co sme stlacili moveDir = new Vector2(moveX, moveY).normalized; } void Move() { rb.velocity = new Vector2(moveDir.x * moveSpeed, moveDir.y * moveSpeed); } }
And this is PlayerAnimator.cs :
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerAnimator : MonoBehaviour { //Referencie Animator am; PlayerMovement pm; SpriteRenderer sr; void Start() { am = GetComponent<Animator>(); pm = GetComponent<PlayerMovement>(); sr = GetComponent<SpriteRenderer>(); } void Update() { if(pm.moveDir.x != 0 || pm.moveDir.y != 0) //ak 0 tak sa nehybe, ak !0 tak sa hybe { am.SetBool("Move", true); SpriteDirectionChecker(); } else { am.SetBool("Move", false); //sleduje ci je to 1 alebo 0, aby nastavil animaciu move } } void SpriteDirectionChecker() { if(pm.moveDir.x < 0) { sr.flipX = true; } else { sr.flipX = false; // ak je X vacsie ako 0 tak doprava, ak viac ako 0 tak dolava sa otoci } } }
And error is: Assets\Scripts\PlayerAnimator.cs(25,37): error CS1002: ; expected
October 26, 2023 at 12:00 pm #12117::Hi sad agent, I had a look at your code. There seems to be nothing wrong with it. It says line 25 of your code is missing a
;
, but there is a semi-colon there. I’ve looked at the rest of your code as well and everything is alright.Can you try running the code again and see if you are getting the same thing?
October 27, 2023 at 2:31 am #12121::I figured it out, not gonna lie, it will sound dumb af, i just deleted the “;” and put it back the next day and it automatically made error disappier
November 28, 2023 at 6:56 am #12325::Terence,
I cannot seem to get this to work either.
Running 2022.3.14f1
PlayerAnimator.cs
<code> using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerAnimator : MonoBehaviour { //References Animator am; PlayerMovement pm; SpriteRenderer sr; void Start() { am = GetComponent<Animator>(); pm = GetComponent<PlayerMovement>(); sr = GetComponent<SpriteRenderer>(); } void Update() { if (pm.moveDir.x != 0 || pm.moveDir.y != 0) { am.SetBool("Move", true); SpriteDirectionChecker(); } else { am.SetBool("Move", false); } print(am.GetBool("Move")); } void SpriteDirectionChecker() { if (pm.lastHorizontalVector < 0) { sr.flipX = true; } else { sr.flipX = false; } } } </code>
PlayerMovement.cs
<code> using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { //Movement public float moveSpeed; [HideInInspector] public Vector2 moveDir; [HideInInspector] public float lastHorizontalVector; [HideInInspector] public float lastVerticalVector; //References Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() { InputManagement(); } void FixedUpdate() { Move(); } void InputManagement() { float moveX = Input.GetAxisRaw("Horizontal"); float moveY = Input.GetAxisRaw("Vertical"); moveDir = new Vector2(moveX, moveY).normalized; if (moveDir.x != 0) { lastHorizontalVector = moveDir.x; } if (moveDir.y != 0) { lastVerticalVector = moveDir.y; } } void Move() { rb.velocity = new Vector2(moveDir.x * moveSpeed, moveDir.y * moveSpeed); } } </code>
November 28, 2023 at 7:05 am #12326::I’ve added in print for the bool “Move” based on another suggestion you had elsewhere, but as a beginner I’m sort of lost.
<code> print(am.GetBool("Move")); </code>
November 28, 2023 at 9:21 pm #12330::Hi Ray, what is the issue you are facing? Is it that the move animation is not playing, or that the player is not flipping?
If you can include a screenshot of your Console window, that will be great as well.
November 29, 2023 at 6:24 am #12333::Hey Terence,
Two issues:
- The player is not flipping
- The player animation does not start paused.
I’m also now on video two and have the the player’s character stuck behind the background tiles. Not sure how to fix that but I can get to it later.
November 29, 2023 at 6:25 am #12334::Also it looks like I do not have anything in the Console. So not sure what’s going on there too lol.
November 30, 2023 at 12:04 pm #12356::Hi Ray, for your character getting stuck behind the background tiles, you have to check your character’s Sprite Renderer and make sure he is in the correct Sorting Layer. You can also try increasing the Order in Layer number on the Sprite Renderer.
As for the character not flipping, add this line to your PlayerAnimator code and show me the Console window when you play the game and try moving around:
void SpriteDirectionChecker() { Debug.Log(pm.lastHorizontalVector); if (pm.lastHorizontalVector < 0) { sr.flipX = true; } else { sr.flipX = false; } }
-
AuthorPosts
- You must be logged in to reply to this topic.