[Novice] Could use help with code.

So, I am Reykur, this is my first post here. Been using Unity for about a week, brand new to coding and game dev in general. Feeling like I’m making solid progress though.
Here’s my issue:
Making a mobile 2D platformer as my first project, and I’m trying to prototype it first. I’m currently working on the character movement so that I can use both the arrow keys on the keyboard, and on screen virtual L/R buttons. I have been using a tutorial on Youtube, and thus far have gotten a character, a simple ground layer, the canvas with mobile buttons on it (a menu button, A/B and L/R). On the Character itself, I have attached the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerManager : MonoBehaviour
{
    public float speedX;
    public float jumpspeedY;

    bool FacingRight, Jumping;
    float speed;

    Animator anim;
    Rigidbody2D rb;

    // Use this for initialization
    void Start ()
    {
        anim = GetComponent<Animator> ();
        rb = GetComponent<Rigidbody2D> ();
        FacingRight = true;
    }
      

    // Update is called once per frame
    void Update ()

    {
        //update for movement controller
        MovePlayer (speed);
        Flip();

        if(Input.GetKeyDown(KeyCode.RightArrow))
        {
            speed = speedX;
        }
        if (Input.GetKeyUp (KeyCode.RightArrow))
        {
            if (speed > 0)
            {
                speed = 0;
            }
        }
        if(Input.GetKeyDown(KeyCode.LeftArrow))
        {
            speed = -speedX;
        }
        if (Input.GetKeyUp (KeyCode.LeftArrow))
        {
            if (speed < 0)
            {
                speed = 0;
            }
        }
        if(Input.GetKeyDown(KeyCode.UpArrow))
        {
            Jumping = true;
            rb.AddForce (new Vector2 (rb.velocity.x, jumpspeedY));
            anim.SetInteger("State", 2);
        }
          
    }
    //code for flipping on left arrow key
    private void Flip()
    {
        if(speed > 0 && !FacingRight || speed < 0 && !FacingRight)
        {
            FacingRight = !FacingRight;

            Vector3 temp = transform.localScale;
            temp.x *= -1;
            transform.localScale = temp;
        }
    }


    //code for player movement
    void MovePlayer(float playerSpeed)
    {
  
        if (playerSpeed < 0 && !Jumping || playerSpeed > 0 && !Jumping)
        {
            anim.SetInteger ("State", 1);
        }
        if (playerSpeed == 0 && !Jumping)
        {
            anim.SetInteger ("State", 0);
        }

        rb.velocity = new Vector3 (speed, rb.velocity.y, 0);

    }
    //collision detection on ground when jumping
    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.tag == "Ground")
        {
            Jumping = false;
            anim.SetInteger ("State", 0);
        }
    }
}

As you may be able to tell, the mobile input has not been coded yet, however the character flip (flip for turning from right to left, or using the L key on the keyboard (as seen in script)) coding doesn’t work. I’ve tried contacting the teacher of the tutorial I am following, with no answer back yet, so I’m not sure if the code needs to be completed (adding the input method for touch screen controls) before the flip will work, or what I’m doing wrong that is causing the script not to function properly. So just to recap the problem as concisely as possible:
How do I get my 2D character’s “playermanager” script to properly flip the character using the described method above?
Any advice on this would be seriously appreciated, if someone tells me to finish the code first then it will happen as so, however I have been fixing problems throughout the tutorial as they come up, and this has been on my plate for too long now, I was Code-Warrior-defeated, and seek aid.
-Reykur

Any chance to use that shouldn’t be passed up. Apologies for the non-useful reply. If nothing else I bumped ya! Bumped ya fa real.

1 Like

I lol’d so I ain’t mad. Making a frustrated person laugh is helpful in it’s own right.
Appreciate the bump though, I’ve been looking through a lot of online resources, but none of them use the method I’ve been using, but rather delegate through the input manager to accomplish movement.

Shamelessly bumping, so that I won’t end up chucking this whole project due to hitting a wall…

Finally Figured out a fix!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerManager : MonoBehaviour
{
    public float speedX;
    public float jumpspeedY;

    bool FacingRight, Jumping;
    float speed;

    Animator anim;
    Rigidbody2D rb;

    // Use this for initialization
    void Start ()
    {
        anim = GetComponent<Animator> ();
        rb = GetComponent<Rigidbody2D> ();
        FacingRight = true;
    }
       

    // Update is called once per frame
    void Update ()

    {
        //update for movement controller
        MovePlayer (speed);

        if(Input.GetKeyDown(KeyCode.RightArrow))
        {
            speed = speedX;
        }
        if (Input.GetKeyUp (KeyCode.RightArrow))
        {
            if (speed > 0)
            {
                speed = 0;
            }
        }
        if(Input.GetKeyDown(KeyCode.LeftArrow))
        {
            speed = -speedX;
            GetComponent<SpriteRenderer>().flipX = true;
        }
        if (Input.GetKeyUp (KeyCode.LeftArrow))
        {
            if (speed < 0)
            {
                speed = 0;
            }
            GetComponent<SpriteRenderer>().flipX = false;
        }
        if(Input.GetKeyDown(KeyCode.UpArrow))
        {
            Jumping = true;
            rb.AddForce (new Vector2 (rb.velocity.x, jumpspeedY));
            anim.SetInteger("State", 2);
        }
           
    }

    //code for player movement anim
    void MovePlayer(float playerSpeed)
    {
   
        if (playerSpeed < 0 && !Jumping || playerSpeed > 0 && !Jumping)
        {
            anim.SetInteger ("State", 1);
        }
        if (playerSpeed == 0 && !Jumping)
        {
            anim.SetInteger ("State", 0);
        }

        rb.velocity = new Vector3 (speed, rb.velocity.y, 0);

    }
    //collision detection on ground when jumping
    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.tag == "Ground")
        {
            Jumping = false;
            anim.SetInteger ("State", 0);
        }
    }
}

I recommend anyone just starting and running into this problem to try and spot the differences for themselves.
Basically I tore out the old Flip() method and put in a flipx=true on keydown and false for keyup. Works properly for what I’m going for, took me 11 hours of research and development to come across the concept that gave me the idea for this implementation. If anyone has a better or more efficient way of doing this then please let me know.

1 Like

Nice job figuring that out!

My only suggestion is: post your novice-level questions in the Getting Started forum. There are quite a few of us (including me) who monitor that forum and look for opportunities to help newbies out. The other forums don’t get quite as much attention for that sort of thing, though they are of course the right place to post advanced topics.

Cheers,

  • Joe