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