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
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);
}
}
}
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!
Yes I’m using rigidbody 2d and box colliders , 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