I’m working on a script that makes a sprite fly between two waypoints. That part works great but I also want the sprite to “flip” so it doesnt fly backwards but the flip part isnt working. I think my issue is with the trigger to make it flip and face the other way but I cant figure it out.
Here is the flyController script that is attached to the sprite:
using UnityEngine;
using System.Collections;
public class FlyController : EnemyController
{
}
here is the EnemyController script
using UnityEngine;
using System.Collections;
public class EnemyController : MonoBehaviour {
[HideInInspector]
public bool isFacingRight = false;
public float maxSpeed = 1.5f;
public void Flip()
{
isFacingRight = !isFacingRight;
Vector3 enemyScale = this.transform.localScale;
enemyScale.x = enemyScale.x * -1;
this.transform.localScale = enemyScale;
}
}
The FlightPoints script that is also assigned to the sprite
using UnityEngine;
using System.Collections;
public class FlightPoints : MonoBehaviour {
public GameObject waypointA;
public GameObject waypointB;
public float speed = 3;
public bool shouldChangeFacing = false;
private bool directionAB = true;
void FixedUpdate()
{
if (this.transform.position == waypointA.transform.position
&& directionAB == false || this.transform.position ==
waypointB.transform.position && directionAB == true)
{
directionAB = !directionAB;
if(this.shouldChangeFacing == true)
{
this.gameObject.GetComponent<EnemyController>().Flip();
}
}
if (directionAB == true)
{
this.transform.position =
Vector3.MoveTowards (this.transform.position,
waypointB.transform.position, speed * Time.fixedDeltaTime);
}
else
{
this.transform.position =
Vector3.MoveTowards (this.transform.position,
waypointA.transform.position, speed * Time.fixedDeltaTime);
}
}
}