MAAC3601-TD-Game/Assets/Scripts/GenerationController.cs

46 lines
1.2 KiB
C#
Raw Normal View History

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 {
public float speed;
private GameObject[] waypoints;
2023-02-16 19:07:21 -04:00
private int waypointIndex = 1;
private Rigidbody2D rb;
2023-02-16 19:07:21 -04:00
private void Start() {
rb = GetComponent<Rigidbody2D>();
moveToNextWaypoint();
2023-02-16 19:07:21 -04:00
}
private void OnTriggerEnter2D(Collider2D col) {
if (col.gameObject == waypoints[waypointIndex]) {
waypointIndex++;
moveToNextWaypoint();
2023-02-16 19:07:21 -04:00
}
2023-02-16 19:07:21 -04:00
}
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);
}
}
2023-02-16 19:07:21 -04:00
}