- Home /
Enemy AI Not Working?
This is my first time attempting Enemy AI and I have no idea if I'm doing it right. This code doesn't work and I just need to be told what I'm doing wrong and how I can fix it:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
private Animator animator;
private GameObject player;
private GameObject enemy;
private float range;
public float speed;
public bool grounded;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
enemy = GameObject.FindGameObjectWithTag("Enemy");
speed = 5f;
}
// Update is called once per frame
void FixedUpdate () {
isGrounded();
}
void Update()
{
range = Vector2.Distance(enemy.transform.position, player.transform.position);
if (range <= 15f)
{
transform.Translate(Vector2.MoveTowards(enemy.transform.position, player.transform.position, range) * speed * Time.deltaTime);
}
}
void isGrounded()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
animator.SetBool("Ground", grounded);
}
}
Comment
Your answer
Follow this Question
Related Questions
Flipping 2d Enemy 1 Answer
Complex Enemy follow AI 1 Answer
How do I implement A* pathfinding to my 2d game, without tiles? 4 Answers
Stop enemy from moving with GetKeyDown 3 Answers
Raycasting AI 0 Answers