Forum begins after the advertisement:


[Part 1] Turning left and right

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #12116
    sad agent
    Level 10
    Former Patron
    Helpful?
    Up
    0
    ::

    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

    #12117
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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?

    #12121
    sad agent
    Level 10
    Former Patron
    Helpful?
    Up
    0
    ::

    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

    #12325
    Ray Lindsey
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    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>
    #12326
    Ray Lindsey
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    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>
    #12330
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

    #12333
    Ray Lindsey
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Hey Terence,

    Unity Engine Screenshot

    Two issues:

    1. The player is not flipping
    2. 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.

    #12334
    Ray Lindsey
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Also it looks like I do not have anything in the Console. So not sure what’s going on there too lol.

    #12356
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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;
        }
    }
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: