2023-02-13 19:38:57 -04:00
|
|
|
using System;
|
2023-02-13 17:50:51 -04:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
2023-02-13 19:38:57 -04:00
|
|
|
public class GenerationController : MonoBehaviour {
|
2023-02-16 23:44:32 -04:00
|
|
|
public float speed;
|
|
|
|
private GameObject[] waypoints;
|
2023-02-16 19:07:21 -04:00
|
|
|
private int waypointIndex = 1;
|
|
|
|
|
|
|
|
private Rigidbody2D rb;
|
2023-02-16 23:44:32 -04:00
|
|
|
|
2023-02-16 19:07:21 -04:00
|
|
|
private void Start() {
|
|
|
|
rb = GetComponent<Rigidbody2D>();
|
2023-02-16 23:44:32 -04:00
|
|
|
moveToNextWaypoint();
|
2023-02-16 19:07:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTriggerEnter2D(Collider2D col) {
|
|
|
|
if (col.gameObject == waypoints[waypointIndex]) {
|
|
|
|
waypointIndex++;
|
2023-02-16 23:44:32 -04:00
|
|
|
moveToNextWaypoint();
|
2023-02-16 19:07:21 -04:00
|
|
|
}
|
2023-02-16 23:44:32 -04:00
|
|
|
|
2023-02-16 19:07:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setWaypoints(GameObject[] waypointsIn) {
|
|
|
|
waypoints = waypointsIn;
|
|
|
|
}
|
2023-02-16 23:44:32 -04:00
|
|
|
|
|
|
|
private void moveToNextWaypoint() {
|
|
|
|
if (waypointIndex < waypoints.Length) {
|
|
|
|
// Point towards the next waypoint
|
|
|
|
var heading = waypoints[waypointIndex].transform.position - transform.position;
|
|
|
|
var distance = heading.magnitude;
|
|
|
|
var direction = heading / distance;
|
|
|
|
var angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
|
|
|
|
var q = Quaternion.Euler(0,0,angle);
|
|
|
|
transform.rotation = q;
|
|
|
|
|
|
|
|
//Move towards the next waypoint, taking into account the speed variable
|
|
|
|
rb.velocity = direction * speed;
|
|
|
|
} else if (waypointIndex >= waypoints.Length) {
|
|
|
|
Destroy(gameObject);
|
|
|
|
}
|
|
|
|
}
|
2023-02-16 19:07:21 -04:00
|
|
|
}
|