- Home /
Enemy AI Help,Help on EnemyAI 2D
I am trying to code a basic Enemy plat-former AI system that shoots when your near its radius and chases you. I have tried multiple solutions that ended up with different bugs. Somehow flying about the player? Violently shaking when coming near the player not moving properly Doesn't flip to face the player correctly
I'm fairly new with Enemy AI chasing and shooting in unity specially.
Here is my code public class EnemyAI : MonoBehaviour { public float speed; public float playerDistance; public float stoppingDistance; public float retreatDistance; public Transform player; public float timeBtwShots; public float startTimeBtwShots; public Vector2 playerXAxis; public bool isFacingRight = false; public int facingDirection = -1; public GameObject projectile; // Start is called before the first frame update void Start() { player = GameObject.FindGameObjectWithTag("Player").transform; playerDistance = Vector2.Distance(player.position, transform.position); playerXAxis = new Vector2(player.position.x, transform.position.y); } // Update is called once per frame void FixedUpdate() { if (playerDistance > stoppingDistance) { transform.position = Vector2.MoveTowards(transform.position, playerXAxis, speed * Time.deltaTime); } else if (playerDistance < stoppingDistance && playerDistance > retreatDistance) { transform.position = this.transform.position; speed = 0; } else if (playerDistance < retreatDistance) { transform.position = Vector2.MoveTowards(transform.position, playerXAxis, -speed * Time.deltaTime); Flip(); } if (timeBtwShots <= 0) { Instantiate(projectile, transform.position, Quaternion.identity); timeBtwShots = startTimeBtwShots; } else { timeBtwShots -= Time.deltaTime; } } void Flip() { facingDirection *= -1; isFacingRight = !isFacingRight; transform.Rotate(0, 180, 0); } }
,So I am making this enemy AI that if you go into a certain distance he will chase you then when close enough he will shoot and If you move towards it he will move back. I have tried multiple solutions he either Shakes violently as i am using a rigid body His movement acts weird He doesn't flip when facing the player and there is a lot of bugs on top of that.
I am fairly new to working with unity AI so I was sorta expecting this but I am not sure how to fix this.
Here is my Code public class EnemyAI : MonoBehaviour { public float speed; public float playerDistance; public float stoppingDistance; public float retreatDistance; public Transform player;
public float timeBtwShots;
public float startTimeBtwShots;
public Vector2 playerXAxis;
public bool isFacingRight = false;
public int facingDirection = -1;
public GameObject projectile;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
playerDistance = Vector2.Distance(player.position, transform.position);
playerXAxis = new Vector2(player.position.x, transform.position.y);
}
// Update is called once per frame
void FixedUpdate()
{
if (playerDistance > stoppingDistance)
{
transform.position = Vector2.MoveTowards(transform.position, playerXAxis, speed * Time.deltaTime);
}
else if (playerDistance < stoppingDistance && playerDistance > retreatDistance)
{
transform.position = this.transform.position;
speed = 0;
}
else if (playerDistance < retreatDistance)
{
transform.position = Vector2.MoveTowards(transform.position, playerXAxis, -speed * Time.deltaTime);
Flip();
}
if (timeBtwShots <= 0)
{
Instantiate(projectile, transform.position, Quaternion.identity);
timeBtwShots = startTimeBtwShots;
}
else {
timeBtwShots -= Time.deltaTime;
}
}
void Flip()
{
facingDirection *= -1;
isFacingRight = !isFacingRight;
transform.Rotate(0, 180, 0);
}
}