EnemyAI
unknown
plain_text
4 years ago
1.0 kB
7
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
public float speed;
private Waypoint Wpoints;
private int waypointIndex;
private void Start()
{
Wpoints = GameObject.FindGameObjectWithTag("Waypoints").GetComponent<Waypoint>();
}
private void Update()
{
transform.position = Vector2.MoveTowards(transform.position, Wpoints.waypoints[waypointIndex].position, speed * Time.deltaTime);
Vector3 dir = Wpoints.waypoints[waypointIndex].position - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
if(Vector2.Distance(transform.position, Wpoints.waypoints[waypointIndex].position) < 0.1f)
{
if(waypointIndex < Wpoints.waypoints.Length - 1)
{
waypointIndex++;
}
}
}
}Editor is loading...