I just started making my 2d platformer game and i would like to change the players sprite from facing right to left when you go either right or left.
public bool IsFacingRight;
void Update () {
//here is your movement controller
//if your player is walking Right, make boolean ------------ IsFacingRight = true;
//if left ------------ IsFacingRight = false;
//if switch direction call funktion for flip ------------- Flip ();
private void Flip ()
{
IsFacingRight = ! IsFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
Thanks For that but im a beginner. i can you know make simple movement, collecting and transportation script but that script i didnt understand alot of, will you please tell me how this works and how i apply a sprite to the script (Sorry for the question am a beginner i just want to learn as much that i can. :-D)
i dont know what controller you use
Open new scene, drag your sprite to the scene. Add to it box collider 2d, add rigidbody2d. Set rigidbodies Gravity Scale 0 (in editor, we have no ground or it fall down). Make c# script with name PlayerMove (or choose name, but change the script public class “PlayerMove” <------ this line ).
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour
{
public bool IsFacingRight = true;
public float Power;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown ("a")) {
rigidbody2D.AddForce (new Vector2 (-Power, rigidbody2D.velocity.y));
if (IsFacingRight) {
IsFacingRight = false;
Flip ();
}
}
if (Input.GetKeyDown ("s")) {
rigidbody2D.AddForce (new Vector2 (Power, rigidbody2D.velocity.y));
if (!IsFacingRight) {
IsFacingRight = true;
Flip ();
}
}
}
private void Flip ()
{
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
add script to your sprite object.
set in unity editor Power 100, check or uncheck IsFacingRight your sprite depended
Try a or s Keys.
if you invert scale x of sprite (set -1), so you can change sprite direction.
or just say what controller you need, so i can try to change the script.