Created
January 24, 2016 13:44
-
-
Save IronMonk-UK/01c54b3f107e4df1c7e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
/* The _Class class is used currently to assign stats to units. When a unit is instantiated, one of the functions below is called, setting the stats for that unit. | |
* I have been recommended to create a class for every unit class in game. At this time, until I have implemented AI further, I will be sticking to this class for unit classes. */ | |
public class _Class : MonoBehaviour { | |
public static _Class cInstance; | |
public static void Barbarian(UserUnit u){ | |
u.health = 15; | |
u.strength = 3; | |
u.defence = 1; | |
u.accuracy = .6f; | |
u.damageRollSides = 8; | |
u.posClass = "Knight"; | |
u.negClass = "Mercenary"; | |
} | |
public static void Knight(UserUnit u){ | |
u._class = "Knight"; | |
u.health = 12; | |
u.strength = 2; | |
u.defence = 2; | |
u.accuracy = .7f; | |
u.damageRollSides = 7; | |
u.posClass = "Mercenary"; | |
u.negClass = "Barbarian"; | |
} | |
public static void Mercenary(UserUnit u){ | |
u._class = "Mercenary"; | |
u.health = 10; | |
u.strength = 2; | |
u.defence = 1; | |
u.accuracy = .9f; | |
u.damageRollSides = 6; | |
u.posClass = "Barbarian"; | |
u.negClass = "Knight"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class GameManager : MonoBehaviour { | |
// Creates a public static variable for the GameManager class | |
// A static class will mean that any changes made to the GameManager will affect all copies of the variable | |
public static GameManager instance; | |
// A game object containing the tile object prefab | |
public GameObject tilePrefab; | |
// A game object containing the prefab for player units | |
public GameObject userUnitPrefab; | |
public Text _name; | |
public Text _class; | |
public Text _health; | |
public Text _strength; | |
public Text _defence; | |
public Text _accuracy; | |
public Text _eName; | |
public Text _eClass; | |
public Text _eHealth; | |
public Text _eStrength; | |
public Text _eDefence; | |
public Text _eAccuracy; | |
// Dictates the width and length of the map | |
public int mapSize; | |
public bool startMove; | |
bool posHit; | |
bool negHit; | |
bool hit; | |
// Creates a list containing a list of tiles for the creation of the map | |
public List<List<Tile>> map = new List<List<Tile>>(); | |
// Creates a list containing players, to populate the game with player units | |
public List<Unit> units = new List<Unit>(); | |
public List<Vector2> moveTiles = new List<Vector2>(); | |
[SerializeField] | |
// How many units are initially in the current Player Index | |
public int currentUnitIndex = 0; | |
void Awake(){ | |
// Makes the instance variable the current GameManager class | |
instance = this; | |
EnemyStatsDown (); | |
} | |
void Start () { | |
// Runs the generateMap function on start | |
generateMap (); | |
// Runs the generatePlayers function on start | |
generateUnits (); | |
} | |
void Update () { | |
/* Looks at which player in the player list is currently moving | |
* and constantly runs the turnUpdate function for them, as it is in the update function */ | |
if (units [currentUnitIndex].health > 0) { | |
units [currentUnitIndex].turnUpdate (); | |
} else { | |
nextTurn (); | |
} | |
// Sets the UI text to show the current units stats | |
_name.text = units [currentUnitIndex]._name; | |
_class.text = units [currentUnitIndex]._class; | |
_health.text = units [currentUnitIndex].health.ToString(); | |
_strength.text = units [currentUnitIndex].strength.ToString(); | |
_defence.text = units [currentUnitIndex].defence.ToString(); | |
_accuracy.text = units [currentUnitIndex].accuracy.ToString(); | |
if (units [currentUnitIndex].moved) | |
units [currentUnitIndex].moving = false; | |
} | |
/* The nextTurn function determines which player is currently having their turn | |
* The current player index starts at 0, so must have +1 added to it when counting players so both start from 1. | |
* When the nextTurn function is run, if the player index + 1 is lower than the amount in the players list | |
* it will increase the index by one, causing the turnUpdate function above to start for a new player. | |
* If the index + 1 ends up equal to the amount of players in the list, it will start back to 0.*/ | |
/* While this is not my ideal turn system, I believe that as I progress with my research, it will help me form a base towards the operation of the AI | |
* As this is one potential way I could have enemy units moving one at a time as an index goes through a list of enemies */ | |
public void nextTurn(){ | |
if (currentUnitIndex + 1 < units.Count) { | |
currentUnitIndex++; | |
} else { | |
currentUnitIndex = 0; | |
} | |
} | |
/* The moveCurrentPlayer function sets the current player units moveDestination position as the destTile, as set when the player clicks on a tile through the Tile class | |
* OnMouseDown function. */ | |
/* The moveCurrentPlayer function now runs through an if statement to make sure the current unit has not already moved. If it has not, they will use one action, and move to the | |
* destTile. This function also sets the current units gridPosition to be that of the destTile's gridPosition, and then sets the moved boolean to true, | |
* so that unit cannot move again. */ | |
public void moveCurrentPlayer(Tile destTile){ | |
if (units [currentUnitIndex].moved == false) { | |
units [currentUnitIndex].actions --; | |
units [currentUnitIndex].moveDestination = destTile.transform.position + Vector3.up; | |
units [currentUnitIndex].gridPosition = destTile.gridPosition; | |
units [currentUnitIndex].moved = true; | |
} | |
} | |
public void attackWithCurrentPlayer(Tile destTile){ | |
// The attackWithCurrentPlayer function sets the rules of combat within the game, dealing with how the currently selected player can engage in combat. | |
/* The variable target is created first. A foreach loop then runs, looking for any unit that has the same gridPosition as the tile being selected, | |
* as rather than targetting a unit, the tile they are on is selected. If a unit is found, they are set as the target. */ | |
Unit target = null; | |
foreach (Unit u in units) { | |
if (u.gridPosition == destTile.gridPosition){ | |
target = u; | |
} | |
} | |
/* So as not to create a giant line of specific gridPositions that can be attacked, I created 4 Vector3 variables, and implemented them into it. | |
* Each Vector3 represents a space above, below, left, or right, of the current units position */ | |
Vector3 up = new Vector2 (target.gridPosition.x, target.gridPosition.y - 1); | |
Vector3 down = new Vector2 (target.gridPosition.x, target.gridPosition.y + 1); | |
Vector3 left = new Vector2 (target.gridPosition.x -1, target.gridPosition.y); | |
Vector3 right = new Vector2 (target.gridPosition.x +1, target.gridPosition.y); | |
/* This if statement starts off by making sure that there is a target available. If so, the next if statement ensures that they are in a position that can be attacked by the | |
* current unit. If this returns true, an action is used by the current unit. A series of if statements are run after this, checking if the units class is weak against them, | |
* strong against them, or the same. At this point in time, units gain or lose accuracy based on this, and sets one of three booleans, based on the classes. | |
* THIS IS SUBJECT TO CHANGE. */ | |
if (target != null) { | |
if(units [currentUnitIndex].gridPosition == up || units [currentUnitIndex].gridPosition == down || units [currentUnitIndex].gridPosition == left || units [currentUnitIndex].gridPosition == right){ | |
units [currentUnitIndex].actions --; | |
if(units [currentUnitIndex].posClass == target._class){ | |
posHit = Random.Range (0.0f, 1.0f) <= (units[currentUnitIndex].accuracy + .2f); | |
}else if(units [currentUnitIndex].negClass == target._class){ | |
negHit = Random.Range (0.0f, 1.0f) <= (units[currentUnitIndex].accuracy - .2f); | |
}else{ | |
hit = Random.Range (0.0f, 1.0f) <= units[currentUnitIndex].accuracy; | |
} | |
/* This set of if statements breaks down the damage dealt to an opposing unit if the unit hits. Based on the boolean activated above, damage will be dealt with | |
* positive or negative modifiers, or no modifers. If the above hit does not return true, the attack simply misses. If the unit being attacked is not on an | |
* adjacent tile, the debug log will return stating the target is not available. */ | |
if(posHit){ | |
int damageAmount = (int)Mathf.Floor(units [currentUnitIndex].strength + Random.Range(2, units [currentUnitIndex].damageRollSides + 2 ) - target.defence); | |
target.health -= damageAmount; | |
Debug.Log (units [currentUnitIndex]._name + " hit " + target._name + " effectively for " + damageAmount + " damage!"); | |
}else if(negHit){ | |
int damageAmount = (int)Mathf.Floor(units [currentUnitIndex].strength + Random.Range(0, units [currentUnitIndex].damageRollSides - 1 ) - target.defence); | |
target.health -= damageAmount; | |
Debug.Log (units [currentUnitIndex]._name + " hit " + target._name + " ineffectively for " + damageAmount + " damage!"); | |
} else if (hit) { | |
int damageAmount = (int)Mathf.Floor(units [currentUnitIndex].strength + Random.Range(0, units [currentUnitIndex].damageRollSides) - target.defence); | |
target.health -= damageAmount; | |
Debug.Log (units [currentUnitIndex]._name + " hit " + target._name + " for " + damageAmount + " damage!"); | |
} else { | |
Debug.Log (target._name + " miss."); | |
} | |
} else { | |
Debug.Log("Target not available"); | |
} | |
} | |
} | |
// The generateMap function is a function set to run on start, creating the map for the game | |
void generateMap (){ | |
// Creates a new map list | |
map = new List<List<Tile>>(); | |
// Runs the statement inside the amount of times equals to the mapsize | |
for (int i = 0; i < mapSize; i++) | |
{ | |
// Declares the Tile lists inside the map list | |
List<Tile> row = new List<Tile>(); | |
// Runs the statement inside the amount of times equals to the mapsize | |
for (int j = 0; j < mapSize; j++) | |
{ | |
/* Creates a new copy of the Tile class while declaring the new variable tile, creating a new copy of the tile prefab. A new vector3 is created, using the current int i minus the | |
* mapSize divided by 2 for x, 0 for y as the map is presently set for a single level, and minus-j plus the mapSize divided by 2 for z. This determines where the tiles are actually going | |
* to spawn based on on global unity co-ordinates. The Mathf.Floor will return the largest integer possible that is smaller or equals to the bracket conditions. The Quaternion.Euler sets | |
* the rotation of the objects, but as they are squares going into a grid, this is left blank as they have no need to be roatated. Finally, the Tile class component is added to the | |
* instantiated prefab. */ | |
Tile tile = ((GameObject)Instantiate(tilePrefab, new Vector3(i - Mathf.Floor(mapSize/2), 0, -j + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<Tile>(); | |
// The new tile is given its gridPosition Vector2 based on what the current int i and int j are | |
tile.gridPosition = new Vector2(i, j); | |
// Adds the tile to the row list | |
row.Add (tile); | |
} | |
// Adds the row list to the map list | |
map.Add(row); | |
} | |
} | |
// The generatePlayers function is used to place the player units into the game | |
void generateUnits (){ | |
// This sets new players added to the players list to use the UserPlayer class | |
UserUnit unit; | |
// Follows the same formula as used for the generateMap function, but instead positions this player unit in the first available grid position, and the one below in the last possible grid position | |
unit = ((GameObject)Instantiate(userUnitPrefab, new Vector3(0 - Mathf.Floor(mapSize/2), 1, -0 + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<UserUnit>(); | |
unit.gridPosition = new Vector2 (0, 0); | |
// A player name is set. THIS IS FOR FUTURE USE ONCE THE UI HAS BEEN FURTHER DEVELOPED. The units _class is also set, changing stats in the Player class Start function. | |
unit._name = "Bentley"; | |
unit._class = "Barbarian"; | |
// The _class function is run to assign stats, depending on the class selected | |
_Class.Barbarian (unit); | |
// Adds the new player to the players list | |
units.Add (unit); | |
unit = ((GameObject)Instantiate(userUnitPrefab, new Vector3((mapSize-1) - Mathf.Floor(mapSize/2), 1, -(mapSize-1) + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<UserUnit>(); | |
unit.gridPosition = new Vector2 (mapSize - 1, mapSize-1); | |
unit._name = "Harrison"; | |
unit._class = "Mercenary"; | |
_Class.Mercenary (unit); | |
units.Add (unit); | |
unit = ((GameObject)Instantiate(userUnitPrefab, new Vector3(4 - Mathf.Floor(mapSize/2), 1, -4 + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<UserUnit>(); | |
unit.gridPosition = new Vector2 (mapSize/2 - 1, mapSize/2 - 1); | |
unit._name = "Alexander"; | |
unit._class = "Knight"; | |
_Class.Knight (unit); | |
units.Add (unit); | |
} | |
// These three functions are for use with the UI buttons, calling on the current unit and changing states within it, so it is either moving, attacking, or its turn has ended. | |
public void Move(){ | |
if (units [currentUnitIndex].moving == false) { | |
units [currentUnitIndex].moving = true; | |
units [currentUnitIndex].attacking = false; | |
} else { | |
units [currentUnitIndex].moving = false; | |
units [currentUnitIndex].attacking = false; | |
} | |
} | |
public void Attack(){ | |
if (units [currentUnitIndex].attacking == false) { | |
units [currentUnitIndex].attacking = true; | |
units [currentUnitIndex].moving = false; | |
} else { | |
units [currentUnitIndex].moving = false; | |
units [currentUnitIndex].attacking = false; | |
} | |
} | |
public void EndTurn(){ | |
units [currentUnitIndex].moving = false; | |
units [currentUnitIndex].attacking = false; | |
units [currentUnitIndex].actions = 2; | |
units [currentUnitIndex].moved = false; | |
nextTurn (); | |
} | |
public void EnemyStatsUp(){ | |
_eName.enabled = true; | |
_eClass.enabled = true; | |
_eHealth.enabled = true; | |
_eStrength.enabled = true; | |
_eDefence.enabled = true; | |
_eAccuracy.enabled = true; | |
} | |
public void EnemyStatsDown(){ | |
_eName.enabled = false; | |
_eClass.enabled = false; | |
_eHealth.enabled = false; | |
_eStrength.enabled = false; | |
_eDefence.enabled = false; | |
_eAccuracy.enabled = false; | |
} | |
/* The MovingTile function is run through the Tile class. If the current tile can be moved to, this function will run, changing that tile to green, indicating that it is part of | |
* the movement path. The bool movingTile is turned true on the Tile class, ensuring that the specific tile knows it is a "moving" tile. The foreach loop then goes through every | |
* tile, setting the prevMovePos to the last tile moved to, so the tiles know which ones can be moved to within the class. */ | |
public void MovingTile(Tile movedTile){ | |
movedTile.rend.material.color = Color.green; | |
startMove = true; | |
movedTile.movingTile = true; | |
foreach (List<Tile> l in map) { | |
foreach (Tile t in l){ | |
t.prevMovePos = movedTile.gridPosition; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Tile : MonoBehaviour { | |
// The gridPosition are Vectors set in the GameManager. | |
public Vector3 gridPosition = Vector2.zero; | |
public Vector3 prevMovePos = Vector2.zero; | |
public Renderer rend; | |
public bool movingTile; | |
void Start () { | |
rend = GetComponent<Renderer> (); | |
} | |
void Update () { | |
} | |
void OnMouseEnter (){ | |
// The below if functions simply change the colour of the tile that the mouse is over, depending on if the current unit is in a moving state, or attacking state. | |
if (GameManager.instance.units [GameManager.instance.currentUnitIndex].moving) { | |
rend.material.color = Color.blue; | |
} else if (GameManager.instance.units [GameManager.instance.currentUnitIndex].attacking) { | |
rend.material.color = Color.red; | |
} | |
// If the tile current being moused over contains a unit, their stats will be displayed in UI text | |
foreach (Unit u in GameManager.instance.units) { | |
if (u.gridPosition == gridPosition && u != GameManager.instance.units [GameManager.instance.currentUnitIndex]) { | |
GameManager.instance.EnemyStatsUp (); | |
GameManager.instance._eName.text = u._name; | |
GameManager.instance._eClass.text = u._class; | |
GameManager.instance._eHealth.text = u.health.ToString (); | |
GameManager.instance._eStrength.text = u.strength.ToString (); | |
GameManager.instance._eDefence.text = u.defence.ToString (); | |
GameManager.instance._eAccuracy.text = u.accuracy.ToString (); | |
} | |
} | |
// This is the if statement that starts the movement function | |
if (GameManager.instance.units [GameManager.instance.currentUnitIndex].moving) { | |
// If a tile has not yet been moved to, the prevMovePos will be set to the current units gridPosition | |
if (!GameManager.instance.startMove) | |
prevMovePos = GameManager.instance.units [GameManager.instance.currentUnitIndex].gridPosition; | |
// 4 Vector3 variables that determine the 4 spaces around the previously moved to tile | |
Vector3 up = new Vector2 (prevMovePos.x, prevMovePos.y - 1); | |
Vector3 down = new Vector2 (prevMovePos.x, prevMovePos.y + 1); | |
Vector3 left = new Vector2 (prevMovePos.x - 1, prevMovePos.y); | |
Vector3 right = new Vector2 (prevMovePos.x + 1, prevMovePos.y); | |
bool placeTile = true; | |
// This foreach statement ensures that a tile with a unit on it cannot be selected as part of the movement path | |
foreach (Unit u in GameManager.instance.units) { | |
if (u.gridPosition == gridPosition) { | |
placeTile = false; | |
break; | |
} | |
} | |
/* Here is where I have been struggling. I am using a vector2 list to store each tile that will be moved to. With my current code, I am able to push the grid positions | |
* into the list, with the issue of two instances of the first position being added. The main issue I am trying to deal with, is so a grid position already added cannot | |
* be added again, as there would be no reason for a unit to cross its own path. | |
* | |
* The issue I have come across is that when the foreach loop below starts, it will only check the first entry in the list, and never look any further. As can be seen, I | |
* have tried multiple loops to overcome this, adding and taking away breaks and continues, in hopes of finding a solution. At this point, I have so far not found the | |
* solution I am seeking. This is my next priority. Afterwards, I will be implementing AI, that will ideally be able to map out paths for themselves. Once I have done | |
* this, I will need to compare the current movement system I have implemented to an A* Pathfinding algorithm, to ensure that I will be using the most effective method | |
* of pathfinding and movement. */ | |
if (placeTile && (gridPosition == up || gridPosition == down || gridPosition == left || gridPosition == right)) { | |
//GameManager.instance.MovingTile (this); | |
if (GameManager.instance.moveTiles.Count == 0) { | |
GameManager.instance.MovingTile (this); | |
GameManager.instance.moveTiles.Add (this.gridPosition); | |
} | |
/*for(int i = 0; i < GameManager.instance.moveTiles.Count; i++){ | |
Vector2 gp = this.gridPosition; | |
if(gp != GameManager.instance.moveTiles[i]){ | |
GameManager.instance.moveTiles.Add (this.gridPosition); | |
break; | |
} | |
}*/ | |
foreach (Vector3 v in GameManager.instance.moveTiles.ToArray()) { | |
Debug.Log (v); | |
if (this.gridPosition != v) { | |
GameManager.instance.MovingTile (this); | |
GameManager.instance.moveTiles.Add (this.gridPosition); | |
break; | |
}else if (GameManager.instance.moveTiles.Count > 1){ | |
GameManager.instance.moveTiles.Remove(v); | |
Debug.Log ("Removed " + v); | |
} | |
} | |
//&& GameManager.instance.moveTiles.Count >= 2 | |
/*foreach (Vector3 v in GameManager.instance.moveTiles.ToArray()) { | |
Debug.Log (v); | |
if ((this.gridPosition.x != v.x) && (this.gridPosition.y != v.y)) { | |
GameManager.instance.moveTiles.Add (this.gridPosition); | |
break; | |
} | |
//if ((this.gridPosition.x == v.x) && (this.gridPosition.y == v.y)) | |
//Debug.Log ("Hit same position."); | |
}*/ | |
} | |
} | |
// A debug log that constantly tells me what the co-ordinates of the grid I have my mouse on are. | |
Debug.Log ("My position is (" + gridPosition.x + "," + gridPosition.y + ")"); | |
} | |
// Sets the tile colour back to white once the mouse has exited it. | |
void OnMouseExit(){ | |
if(!movingTile) | |
rend.material.color = Color.white; | |
foreach (Unit u in GameManager.instance.units) { | |
if(u.gridPosition == gridPosition){ | |
GameManager.instance.EnemyStatsDown(); | |
} | |
} | |
} | |
void OnMouseDown(){ | |
// The if function makes sure that the current unit is in a moving state before running the moveCurrentPlayer function in the GameManager. | |
if (GameManager.instance.units [GameManager.instance.currentUnitIndex].moving) | |
GameManager.instance.moveCurrentPlayer (this); | |
// Before reaching it in the tutorial, I noticed that the unit gridPosition was not being set | |
// So this was my own take on applying new grid positioning to the unit | |
// This was changed when the moved variable was added | |
// GameManager.instance.players [GameManager.instance.currentPlayerIndex].gridPosition = gridPosition; | |
else if (GameManager.instance.units [GameManager.instance.currentUnitIndex].attacking) | |
GameManager.instance.attackWithCurrentPlayer (this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Unit : MonoBehaviour { | |
public Vector3 moveDestination; | |
public Vector3 gridPosition = Vector2.zero; | |
public bool moving = false; | |
public bool moved = false; | |
public bool attacking = false; | |
public int moveSpeed = 1; | |
public int actions = 2; | |
public int health; | |
public int strength; | |
public int defence; | |
public float accuracy; | |
public float damageRollSides; | |
public string _name; | |
public string _class; | |
public string posClass; | |
public string negClass; | |
void Start () { | |
// Holds the moveDestination position for the current player. | |
moveDestination = transform.position; | |
} | |
void Update () { | |
} | |
// By being a virtual function, this allows child classes to override this function. | |
public virtual void turnUpdate(){ | |
// This if function makes sure that when a unit, either player or AI controlled, has used all its actions, the actions counter, and movement state booleans are reset. | |
if (actions <= 0) { | |
actions = 2; | |
moving = false; | |
attacking = false; | |
moved = false; | |
GameManager.instance.nextTurn(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// Sets the class to work off the player class, rather than being a monoBehaviour class. | |
public class UserUnit : Unit { | |
Renderer rend; | |
// Makes sure the rend variable is set to the units renderer, and ensures that moving and attacking states are both set to false. | |
void Awake (){ | |
rend = GetComponent<Renderer> (); | |
moving = false; | |
attacking = false; | |
} | |
void Update () { | |
// This if statement sets any user unit that is currently active to green, so it is clear which unit at the time can be moved. | |
if (GameManager.instance.units [GameManager.instance.currentUnitIndex] == this) | |
{ | |
rend.material.color = Color.green; | |
} else { | |
rend.material.color = Color.white; | |
} | |
// This statement sets the unit to be red, and fall on its side, once it has run out of health. | |
// NOTE TO SELF: Make sure that any dead unit cannot be set as an active unit again. | |
if (health <= 0) { | |
transform.rotation = Quaternion.Euler(new Vector3(90,0,0)); | |
rend.material.color = Color.red; | |
} | |
} | |
// This allows the child class UserPlayer to override what is in the base Player class' turnUpdate function. | |
public override void turnUpdate(){ | |
// Looks to see if the distance between the moveDestination, as set in the GameManger class through the Tile class, is further than 0.1 away from the current unit position. | |
if (Vector3.Distance (moveDestination, transform.position) >0.1f) | |
{ | |
// Starts the unit moving towards their destination. | |
transform.position += (moveDestination - transform.position).normalized * moveSpeed * Time.deltaTime; | |
// Looks to see if the unit has reached its destination by confirming it is less that 0.1 away. | |
if (Vector3.Distance(moveDestination, transform.position) <= 0.1f) | |
{ | |
// Sets the current position as the moveDestination, so it stays in position. | |
transform.position = moveDestination; | |
} | |
} | |
// This calls on the Player class turnUpdate function to run with the UserPlayer turnUpdate. | |
base.turnUpdate (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment