Flipping 2D Character Animation

Hello,
I am new to scripting and I’m working on a 2d game that consists of the main character always moving forward and flip when he hits a wall, I manage to script that but the character’s sprite isn’t flipping in the opposite direction of the wall that he bumps into.

Any help would be greatly appreciated, I’m using Unity 5.1.

Thank you for taking your time to read this thread :slight_smile:

How are you doing the flipping now?

Basically, I have made a wall with 1 box collider on the left half and attached a gameobject with another box collider on the right side. Then I added a “flip” and “flip2” to the corresponding sides.

Here’s the script

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour

{
public float power = 5;

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

{
transform.Translate (Vector2.right * power * Time.deltaTime);
}
void OnCollisionStay2D(Collision2D coll)
{
if (coll.gameObject.tag == “flip”)
{
power = -5;
}
if (coll.gameObject.tag == “flip2”)
{
power = 5;
}
}
}

Basically , we flip the localScale of the sprite to negative or positive. Interchange the -1 and 1 value that I used, if your character sprite is facing to the left side.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
public float power =5;

void Update()
{

transform.Translate(Vector2.right * power *Time.deltaTime);

}

void OnCollisionStay2D(Collision2D coll)
{

if (coll.gameObject.tag =="flip")
{

power =-5;

transform.localScale =new Vector3(-1, transform.localScale.y, transform.localScale.z);

}

if (coll.gameObject.tag =="flip2")
{

power =5;

transform.localScale =new Vector3(1, transform.localScale.y, transform.localScale.z);

}

}

}
1 Like

Thank you so much!

If you don’t mind helping me a bit more… How could I add a jump to the character? Sorry if it seems simple, I’m new to scripting and Unity and I am really trying my best!

You should really look at some tutorials, they will help a lot! :slight_smile:

Are you using the physics system at the moment? Rigidbody, colliders?

Yes I’m using rigidbody 2d and box colliders :slight_smile: , I am looking at tutorials, but it confuses me because I’m using unity 5.1 and when I am learning the script and doing the same, its not working because the version used in the tutorial is like 4.3 - 4.6 :confused: