My enemy runs backwards away from my player object instead of towards it.
My enemy runs backwards away from my player object instead of towards it, i really can't figure it out, i've tried flipping some of the digits in the code below and it doesn't seem to have changed anything.
using UnityEngine; using System.Collections;
public class EnemyAIMovementLeftController : MonoBehaviour {
public float enemySpeed;
Animator enemyAnimator;
public GameObject enemyGraphic;
bool canFlip = true;
bool facingRight = false;
float flipTime = 5f;
float nextFlipChance = 0f;
public float chargeTime;
float startChargeTime;
bool moving;
Rigidbody2D enemyRB;
void Start(){
enemyAnimator = GetComponentInParent<Animator> ();
enemyRB = GetComponentInParent<Rigidbody2D> ();
}
void Update(){
/*if (Time.time > nextFlipChance) {
if (Random.Range (0, 10) >= 5) {
flipFacing ();
}
nextFlipChance = Time.time + flipTime;
}*/
}
void OnTriggerEnter2D(Collider2D Other){
if (Other.tag == "Player") {
/*if (facingRight && Other.transform.position.x < transform.position.x) {
flipFacing ();
} else if (!facingRight && Other.transform.position.x > transform.position.x) {
flipFacing ();
}
canFlip = false;*/
moving = true;
startChargeTime = Time.time + chargeTime;
}
}
void OnTriggerStay2D(Collider2D Other){
if (Other.tag == "Player") {
if (startChargeTime < Time.time) {
if (!facingRight) {
enemyRB.AddForce (new Vector2 (1, 0) * enemySpeed);
} else {
enemyRB.AddForce (new Vector2 (-1, 0) * enemySpeed);
}
enemyAnimator.SetBool ("Moving", moving);
}
}
}
void OnTriggerExit2D(Collider2D Other){
if (Other.tag == "Player") {
canFlip = true;
moving = false;
enemyRB.velocity = new Vector2 (0f, 0f);
enemyAnimator.SetBool ("Moving", moving);
}
}
void flipFacing(){
if (!canFlip) {
return;
}
float facingX = enemyGraphic.transform.localScale.x;
facingX *= -1f;
enemyGraphic.transform.localScale = new Vector3(facingX, enemyGraphic.transform.localScale.y, enemyGraphic.transform.localScale.z);
facingRight = !facingRight;
}
}
Comment
Your answer
Follow this Question
Related Questions
Trouble making jumping spider enemies 0 Answers
Spawned enemy walk from right to left destroying turrets. 0 Answers
Unity C# - How to check if random moving gameobject is within boundaries 0 Answers
Trying to add a tracker to enemy AI either prefab or with reference 0 Answers
Character continues moving when locked onto an enemy 1 Answer