why does my enemy attack backwards?
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 (); 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;
}
} }
thanks in advance!
Your answer
Follow this Question
Related Questions
Basic Enemy follow script for Unity five, desperately needed! 0 Answers
My enemy is not moving 0 Answers
2D How to get enemy to face the player while moving 0 Answers
Change Enemy Direction 1 Answer