-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharacterBehaviour.cs
58 lines (51 loc) · 2.47 KB
/
CharacterBehaviour.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterBehaviour : MonoBehaviour {
private Stack<GameObject> _currentPath = new Stack<GameObject>();
[Header("Movement Settings")]
public int MaxMovements = 100;
public float MoveSpeed = 10;
void Update() {
// check movement stack to see if we need to move the character
if (_currentPath.Count > 0) {
// peek at the next targets location & move towards
var target = _currentPath.Peek();
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, MoveSpeed * Time.deltaTime);
// remove from the path stack when we reach desired location
if (target.transform.position.x == gameObject.transform.position.x && target.transform.position.z == gameObject.transform.position.z) {
_currentPath.Pop();
}
}
}
public void MoveToPosition(int toX, int toY) {
// find our grid manager in our scene
var gridManager = GameObject.FindGameObjectWithTag("GridManager");
if (gridManager) {
// use our grid manager to calculate the best route to a specific position
var path = gridManager.GetComponent<GridBehaviour>().GetPathToPosition(gameObject.transform, toX, toY, MaxMovements);
for (int i = 0; i < path.Count; i++) {
// push the values into our stack
// then our update function within this script will begin to move our character!
_currentPath.Push(path[i]);
}
} else {
print("Could not find GridManager object within scene.");
}
}
public void MoveToPosition(Transform targetPosition) {
// find our grid manager in our scene
var gridManager = GameObject.FindGameObjectWithTag("GridManager");
if (gridManager) {
// use our grid manager to calculate the best route to a specific position
var path = gridManager.GetComponent<GridBehaviour>().GetPathToPosition(gameObject.transform, targetPosition, MaxMovements);
for (int i = 0; i < path.Count; i++) {
// push the values into our stack
// then our update function within this script will begin to move our character!
_currentPath.Push(path[i]);
}
} else {
print("Could not find GridManager object within scene.");
}
}
}