- 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.
Is there a way to rig something up that the animation will mirror when he reaches the end of his march 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;
5.
public class ObjectMove : MonoBehaviour
{
public float amounttomovex;
public float speed;
10. private float currentposx;
private float currentposy;
private int facing;
void Start()
15. {
currentposx = gameObject.transform.position.x;
facing = 0;
}
20. void Update()
{
if (facing == 1 && gameObject.transform.position.x < currentposx - amounttomovex)
{
facing = 0;
25. }
if (facing == 0 && gameObject.transform.position.x > currentposx)
{
facing = 1;
30. }
if (facing == 0)
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
35. }
else if (facing == 1)
{
transform.Translate(-Vector2.right * speed * Time.deltaTime);
}
40. }
}
The else if is the problem area for it is when the player has reached the end of the desired march.
Answer by K_Tec · Jun 26, 2017 at 07:55 PM
Hello, Sure this is possible. First you need a second animation (thats the easyest way) Then you add a Animation Component to your Character, and put the 2 anims into the array. Then you create a var:
public Animation anim; //Make it a var because its better for performance than GetComponent<>();
And then when at the if(), were your player is turing around and walking back you write
anim.CrossFade("YourAnimationName"); // the right to left anim
and at the other if were hes turning again to walk from left to right you write this too
anim.CrossFade("YourAnimationName"); //left to right anim
I hope i could help you!!
Have a good time!
Answer by andrewwebber25 · Jun 27, 2017 at 08:41 PM
@12tttttOkay thanks for the help but im still having issues. Now the enemy doesnt move at all. I created two animations. One called Left the other Right. I added an Animation Component to the enemy and set the animation count to 2 (so I could attach two animations to it). I attached Left to Element 0 and Right to Element 1. Now on my HazardMove script I put in this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HazardMove : MonoBehaviour{
private ObjectMove player;
public Transform start;
public float amounttomovex;
public float speed;
private float currentposx;
private float currentposy;
private int facing;
public Animation anim;
void Start()
{
player = FindObjectOfType<ObjectMove>();
currentposx = gameObject.transform.position.x;
facing = 0;
}
void Update()
{
if (facing == 1 && gameObject.transform.position.x < currentposx - amounttomovex)
{
facing = 0;
anim.CrossFade("Left");
}
if (facing == 0 && gameObject.transform.position.x > currentposx)
{
facing = 1;
anim.CrossFade("Right");
}
if (facing == 0)
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
else if (facing == 1 && gameObject.transform.position.x == currentposx) //This will make the enemy mirror image when turning direction.
{
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
transform.Translate(-Vector2.right * speed * Time.deltaTime);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
player.transform.position = start.position;
}
}
}
Is there something wrong with how I placed the animation code in there? I even took out the above code "facing = 0" and "facing = 1" because I wasnt sure if it was interfering. Should they be in other spots?
No errors when Playing or Debugging the script.
Your answer
Follow this Question
Related Questions
How to make an enemy pace? 1 Answer
Player Is Not Facing Right/Left When Root Motion Is Enabled 0 Answers
2d character stuck in jump 0 Answers
How To Mirror Animation 1 Answer