I’ve been working on a game for a school project and I’m currently trying to figure out a basic enemy AI. Right now I’m using a MoveTowards to make the enemy walk towards the player when the player is within range, and return to it’s spawnpoint when the player is out of range or dead.
public float maxSpeed = 10;
private bool grounded = false;
public Transform groundCheck;
private float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
public Transform target;
public Transform spawn;
void FixedUpdate () {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
animate.SetBool ("Ground",grounded);
animate.SetFloat ("vSpeed", rb.velocity.y);
float step = maxSpeed * Time.deltaTime;
rb.velocity = new Vector2 (step * maxSpeed, rb.velocity.y);
animate.SetFloat("Speed", Mathf.Abs(rb.velocity.y));
}
void OnTriggerStay2D (Collider2D other){
float step = maxSpeed;
if (other.gameObject.CompareTag ("target")) {
transform.position = Vector2.MoveTowards (transform.position, target.position, step);
}
}
This is the code that currently controls my enemy. When my player is within range of my enemy, it flies towards the player, kills it, and hovers back to it’s spawn. Any help would be appreciated!