- Home /
Touch Controls for Player Movement not Working Properly
Hey so this is a bit of a loaded question so theres a good chance that im going to award some points if anyone can figure this out. So I made a 2D sidescroller that up until now i have used the keyboard to control left and right movement with GetAxis ("Horizontal"). The player walks left and right fine and animates properly. Meaning that if I walk left, he actually faces and walks the direction.
Im developing this game for android phones so now I made some touch buttons on the canvas. They work well and when pressed the player walks left and right. My only issue is that when he walks left, instead of walking facing that direction, he looks like he is walking backward/moonwalking. How can I get it to play the mirrored animation like when he uses keyboard commands? Here is my code for the Player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
public bool grounded;
private Rigidbody2D rb2d;
private Animator anim;
public bool moveright;
public bool moveleft;
void Start()
{
rb2d = gameObject.GetComponent<Rigidbody2D>();
anim = gameObject.GetComponent<Animator>();
}
void Update()
{
// This code is used to move the player with keyboard
anim.SetBool("Grounded", grounded);
anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));
if (Input.GetAxis("Horizontal") < -0.1)
{
transform.localScale = new Vector3(-1, 1, 1);
}
if (Input.GetAxis("Horizontal") > 0.1)
{
transform.localScale = new Vector3(1, 1, 1);
}
if (Input.GetButtonDown("Jump") && grounded)
{
rb2d.AddForce(Vector2.up * jumpPower);
}
//This code is using the bools from above to make player move by touch icons
if (Input.GetKey(KeyCode.LeftArrow))
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}
if (Input.GetKey(KeyCode.RightArrow))
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
}
if (moveright)
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
}
if (moveleft)
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}
}
void FixedUpdate()
{
Vector3 easeVelocity = rb2d.velocity;
easeVelocity.y = rb2d.velocity.y;
easeVelocity.z = 0.0f;
easeVelocity.x *= 0.75f;
float h = Input.GetAxis("Horizontal");
//Fake friction / Easing the x speed of our player
if (grounded)
{
rb2d.velocity = easeVelocity;
}
//Moving the Player
rb2d.AddForce((Vector2.right * speed) * h);
//Limiting the Speed of the Player
if (rb2d.velocity.x > maxSpeed)
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
}
if (rb2d.velocity.x < -maxSpeed)
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}
}
}
I originally tried replacing the "rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);" code with "transform.localScale = new Vector3(-1, 1, 1);" which made the player face the proper directions with the touch arrows but stopped him from walking. For all I know, which direction the player animates isnt even part of this script, its been so long since Ive done it. Im stumped really hard here guys and I know this is a loaded question with a lot of code but Ill take any suggestions or alternative methods if you can think of any! Thanks
Answer by FortisVenaliter · Jul 27, 2017 at 08:18 PM
You tried replacing the rigidbody code with the scale code? Try merging them. Use both statements within the if block instead of one or the other.
woah i feel kind of stupid, how didnt i think of that?! Thank you very much for the quick fix!