Pressing the key once, moving up, down, left or right until meet the wall.
Hi, I'm trying to make a clone of a new game for iOS called Tomb of the Mask. I want to achieve the same movement. Example: https://youtu.be/9Acc6VwN-4E?t=37
I've managed to do that using gravity and it's working just fine
public float gravityUp = 100.0f;
public float gravityDown = (-100.0f);
public float gravityRight = 100.0f;
public float gravityLeft = (-100.0f);
void Start ()
{
rb2d = GetComponent<Rigidbody2D> ();
}
void FixedUpdate ()
{
Vector2 GravityUp = new Vector2 (0.0f, gravityUp);
Vector2 GravityDown = new Vector2 (0.0f, gravityDown);
Vector2 GravityLeft = new Vector2 (gravityLeft, 0.0f);
Vector2 GravityRight = new Vector2 (gravityRight, 0.0f);
if ((Input.GetKey (KeyCode.W)) && (rb2d.velocity == (new Vector2(0, 0))) ) {
Physics2D.gravity = GravityUp;
rb2d.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
}
else if ((Input.GetKey (KeyCode.S)) && (rb2d.velocity == (new Vector2(0, 0))) ) {
Physics2D.gravity = GravityDown;
rb2d.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
}
else if ((Input.GetKey (KeyCode.A)) && (rb2d.velocity == (new Vector2(0, 0))) ) {
Physics2D.gravity = GravityLeft;
rb2d.constraints = RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezeRotation;
}
else if ((Input.GetKey (KeyCode.D)) && (rb2d.velocity == (new Vector2(0, 0))) ) {
Physics2D.gravity = GravityRight;
rb2d.constraints = RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezeRotation;
}
}
but there's another problem: I want the character to play animation of flying right when flying right, play animation of flying left when flying left etc. Maybe the solution is OK and I only need to make it work somehow in the animator? I know I can flip the character when flying up and down but this wont work for going left and right. I thought about getting the horizontal and vertical velocity thinking x will be negative when going left etc. but both of them will be positive I think. It's for my coursework and I'm going nuts trying to make it work.
Thank you all.