Add pathing for enemies
This commit is contained in:
@ -1,17 +1,5 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
public class ArtistController : MonoBehaviour {
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameController : MonoBehaviour
|
||||
{
|
||||
public GameObject artist;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0)) {
|
||||
Instantiate(artist, Camera.main.ScreenToWorldPoint(Input.mousePosition) + Vector3.forward, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,44 +4,37 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class GenerationController : MonoBehaviour {
|
||||
internal GameObject[] waypoints;
|
||||
private int waypointIndex = 1;
|
||||
|
||||
private Rigidbody2D rb;
|
||||
|
||||
public GameObject PathGameObject;
|
||||
private Rigidbody2D rb;
|
||||
// Start is called before the first frame update
|
||||
private void Start() {
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
int pathIndex = 0;
|
||||
// Update is called once per frame
|
||||
void Update() { }
|
||||
|
||||
bool moving = false;
|
||||
private void FixedUpdate() {
|
||||
if (waypointIndex < waypoints.Length) {
|
||||
var target = waypoints[waypointIndex].transform.position;
|
||||
var direction = target - transform.position;
|
||||
var f = transform.position + direction.normalized * 0.1f;
|
||||
rb.MovePosition(f);
|
||||
} else if (waypointIndex >= waypoints.Length) {
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private GameObject pathPointer;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
PathGameObject = GameObject.Find("Path");
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void FixedUpdate() {
|
||||
pathPointer = PathGameObject.GetComponent<PathingController>().waypointList[pathIndex] as GameObject;
|
||||
|
||||
if (!moving) {
|
||||
Vector2 force = new Vector2(pathPointer.transform.position.x, pathPointer.transform.position.y);
|
||||
rb.AddForce(force);
|
||||
moving = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D col) {
|
||||
if (col.gameObject == pathPointer) {
|
||||
moving = false;
|
||||
pathIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void OnTriggerEnter2D(Collider2D col) {
|
||||
if (col.gameObject == waypoints[waypointIndex]) {
|
||||
waypointIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
public void setWaypoints(GameObject[] waypointsIn) {
|
||||
waypoints = waypointsIn;
|
||||
}
|
||||
}
|
31
Assets/Scripts/LevelController.cs
Normal file
31
Assets/Scripts/LevelController.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public class LevelController : MonoBehaviour {
|
||||
public GameObject artist;
|
||||
public GameObject enemy;
|
||||
public GameObject[] waypoints;
|
||||
|
||||
|
||||
private Queue<GameObject> enemies = new();
|
||||
// Start is called before the first frame update
|
||||
void Start() {
|
||||
spawnEnemy(enemy, waypoints[0].transform.position);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update() {
|
||||
if (Input.GetMouseButtonDown(0)) {
|
||||
Instantiate(artist, Camera.main.ScreenToWorldPoint(Input.mousePosition) + Vector3.forward, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
|
||||
private void spawnEnemy(GameObject enemyIn, Vector3 position) {
|
||||
var e = Instantiate(enemyIn, position, Quaternion.identity);
|
||||
var ec = e.GetComponent<GenerationController>();
|
||||
ec.setWaypoints(waypoints);
|
||||
enemies.Enqueue(e);
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 709edaf88476e014f9de790745097efd
|
||||
guid: 29744b92ef0fa824ba2c2dfaecd784ed
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
@ -1,44 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PathingController : MonoBehaviour {
|
||||
|
||||
public GameObject waypointPrefab;
|
||||
public ArrayList waypointList = new ArrayList();
|
||||
|
||||
public GameObject enemyPrefab;
|
||||
|
||||
private ArrayList enemyList = new ArrayList();
|
||||
|
||||
int counter = 0;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
waypointList.Add(Instantiate(waypointPrefab, new Vector3(0, 0, 0), Quaternion.identity));
|
||||
waypointList.Add(Instantiate(waypointPrefab, new Vector3(0, 5, 0), Quaternion.identity));
|
||||
waypointList.Add(Instantiate(waypointPrefab, new Vector3(5, 5, 0), Quaternion.identity));
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void FixedUpdate() {
|
||||
if (counter == 0) {
|
||||
enemyList.Add(Instantiate(enemyPrefab));
|
||||
counter++;
|
||||
}
|
||||
else if (counter >= 50) {
|
||||
counter = 0;
|
||||
}
|
||||
else {
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
8
Assets/Scripts/WaypointController.cs
Normal file
8
Assets/Scripts/WaypointController.cs
Normal file
@ -0,0 +1,8 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class WaypointController : MonoBehaviour {
|
||||
private void Start() {
|
||||
var transparent = new Color(1, 1, 1, 0);
|
||||
GetComponent<SpriteRenderer>().color = transparent;
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 62f8cdc29b6f43e4998a2750b3b9a023
|
||||
guid: 2346c6ef8f7f55541b5f27987d960a46
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
Reference in New Issue
Block a user