46 lines
1.2 KiB
C#
46 lines
1.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class GenerationController : MonoBehaviour {
|
|
public float speed;
|
|
private GameObject[] waypoints;
|
|
private int waypointIndex = 1;
|
|
|
|
private Rigidbody2D rb;
|
|
|
|
private void Start() {
|
|
rb = GetComponent<Rigidbody2D>();
|
|
moveToNextWaypoint();
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D col) {
|
|
if (col.gameObject == waypoints[waypointIndex]) {
|
|
waypointIndex++;
|
|
moveToNextWaypoint();
|
|
}
|
|
|
|
}
|
|
|
|
public void setWaypoints(GameObject[] waypointsIn) {
|
|
waypoints = waypointsIn;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |