My enemy attacks backwards, and i'm not sure why it does this
my enemy is supposed to launch an attack at the player whenever the player comes within range, but the attack fires from the wrong side of the enemy every time. below is the code that controls the enemy attacking.
using UnityEngine; using System.Collections;
public class ShootAtPlayerInRange : MonoBehaviour {
public float playerRange;
public GameObject attackProjectile;
public CharacterController2D player;
public Transform launchPointRight;
public Transform launchPointLeft;
public float waitBetweenShots;
private float shotCounter;
// Use this for initialization
void Start () {
player = FindObjectOfType<CharacterController2D> ();
shotCounter = waitBetweenShots;
}
// Update is called once per frame
void Update () {
shotCounter -= Time.deltaTime;
Debug.DrawLine (new Vector3 (transform.position.x - playerRange, transform.position.y, transform.position.z), new Vector3 (transform.position.x + playerRange, transform.position.y, transform.position.z));
if (transform.localScale.x < 0 && player.transform.position.x > transform.position.x && player.transform.position.x < transform.position.x + playerRange && shotCounter < 0) {
Instantiate (attackProjectile, launchPointRight.position, launchPointRight.rotation);
shotCounter = waitBetweenShots;
}
if (transform.localScale.x > 0 && player.transform.position.x < transform.position.x && player.transform.position.x > transform.position.x - playerRange && shotCounter < 0) {
Instantiate (attackProjectile, launchPointLeft.position, launchPointLeft.rotation);
shotCounter = waitBetweenShots;
}
}
}
Comment