- Home /
Could someone please help with making an enemy sprite flip when going the other way?
Hi I am in the process of making a 2d platformer and have got a simple enemy sprite who I have managed to get moving back and forth, but I am really having issues with getting the sprite to flip when going the other direction. I have tried various different ways but I have a feeling that the movement script I am using makes it difficult. I have spent the last 4 hours trying to get this to work and have become very frustrated! This is the script I am using to move him and I have tried adding a flip function but the sprite goes crazy when he reaches the point where he should flip. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GroundEnemyMove : MonoBehaviour
{
private Vector3 posA;
private Vector3 posB;
private Vector3 nextpos;
public bool facingLeft;
[SerializeField]
public float speed;
[SerializeField]
private Transform Enemy;
[SerializeField]
private Transform transformB;
// Use this for initialization
void Start()
{
posA = Enemy.localPosition;
posB = transformB.localPosition;
nextpos = posB;
}
// Update is called once per frame
void Update()
{
Move();
}
void Flip()
{
facingLeft = !facingLeft;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
private void Move()
{
Enemy.localPosition = Vector3.MoveTowards(Enemy.localPosition, nextpos, speed * Time.deltaTime);
if (Vector3.Distance(Enemy.localPosition, nextpos) <= 0.1)
{
ChangeDestination();
}
}
private void ChangeDestination()
{
nextpos = nextpos != posA ? posA : posB;
transform.position = nextpos;
Flip();
}
}
Answer by The_Magical_Muffins · Mar 26, 2017 at 08:15 PM
Have you checked here? https://docs.unity3d.com/ScriptReference/SpriteRenderer-flipX.html
I would try something along the lines of, If the enemy has a velocity below 0(he would move left), then flip the image
Thanks for the response I tried to play around with your suggestion but it became more complicated as I needed to add RigidBody for the direction and I couldn't get it to work. I have managed it now based from your suggestion with the SpriteRenderer and incorporating it into my existing flip function.
void Flip()// This makes him turn at the end of each distance
{
if (nextpos == posA)
{
groundenemy.flipX = true;
}
if (nextpos == posB)
{
groundenemy.flipX = false;
}
Thanks for the response. I tried to play around with your suggestion but found myself getting bogged down in rigidbody2d and it seemed to get more and more complicated (and still didn't work!). I have managed it now though using the sprite renderer and the positioning.
void Flip()// This makes him at the end of each distance { if (nextpos == posA) { groundenemy.flipX = true; } if (nextpos == posB) { groundenemy.flipX = false; }
@ The_$$anonymous$$agical_$$anonymous$$uffins
Your answer
Follow this Question
Related Questions
sprite always flip to left,Sprite always flip to left 1 Answer
export max to unity ??? 2 Answers
Script not obeying? 2 Answers
Unity not recognizing true statement. 1 Answer
How to get an object to turn around/flip at a certain point 2 Answers