- Home /
Mirroring Animation with Object Move
Hey guys so I started adding my first bad guy to my 2D platformer. So far he works perfect, I added an ObjectMove script to him that allows me to adjust (in inspector view) how far he can pace back and forth and at what speed he does it. I have a sprite animation that makes him look like he is walking from left to right (while technically still being in place). This animation plays continuously as he walks back and forth. So basically half of the time he is walking from left to right as he should and then when he paces back from right to left, he looks like hes moon walking.
If I set his move distance to 6 for example, is there a way to rig something up that the animation will mirror when he reaches the end of his distance, to make it look like he is turning around? I actually think this is a pretty difficult task and some people were saying you cant do it but I figured I would ask. Im also willing to just flip all the sprites in the animation, make a second animation and have that one played at the end of the first ones cycle if that is possible. Thanks for the help guys!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectMove : MonoBehaviour
{
public float amounttomovex;
public float speed;
private float currentposx;
private float currentposy;
private int facing;
void Start()
{
currentposx = gameObject.transform.position.x;
facing = 0;
}
void Update()
{
if (facing == 1 && gameObject.transform.position.x < currentposx - amounttomovex)
{
facing = 0;
}
if (facing == 0 && gameObject.transform.position.x > currentposx)
{
facing = 1;
}
if (facing == 0)
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
else if (facing == 1)
{
transform.Translate(-Vector2.right * speed * Time.deltaTime);
}
}
}
Answer by VinceC80 · Jun 23, 2017 at 08:28 PM
Maybe I'm oversimplifying, but can't you just set the Y scale to a negative value to flip everything?
else if (facing == 1)
{
transform.localScale = new Vector3(1, -1, 1);
transform.Translate(-Vector2.right * speed * Time.deltaTime);
}
Hmm didn't seem to work quite. I took your code and subbed it for my last "else if" statement and some weird stuff happened. The enemy grew about 5x its size, rotated 180 degrees so it looked like he was walking on his head and still only animated in one direction. I tried putting the -1 for x ins$$anonymous$$d of the y scale and it didn't make a difference
You are right, it should be X scale, not Y. Give this a shot, this should retain the current scale (and not enlarge your object).
else if (facing == 1)
{
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
transform.Translate(-Vector2.right * speed * Time.deltaTime);
}
Its getting a little closer. So when he walks from right to left the image is mirroring repeatedly very fast the whole time. As soon as he walks all the way left and starts walking back to the right, he walks normal, sometimes he is facing the correct direction, sometimes he is moonwalking (the scale stays either .1 or -.1). Then when he walks right as far as he can, he starts walking left while vigorously mirror swaps the whole time again (constantly swapping -.1 and .1 until he reaches the end of the walk). This keeps going till I stop it :/
Answer by andrewwebber25 · Jun 26, 2017 at 07:25 PM
Moderators Please Feel free to delete this question!