- Home /
Isometric Move to nearest Node
SO this node system currently is suitable for a Tower Defense game where nodes are predefined in an Array and move to the next in order. How would I implement a feature to move to the nearest node to the character within a range x number of tiles?
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(IsoObject))]
[ExecuteInEditMode]
public class Node : MonoBehaviour {
[SerializeField]
private IsoObject obj;
public Vector3 Position {
get { return obj.Position; }
private set { obj.Position = value; }
}
void Start() {
obj = GetComponent<IsoObject>();
}
void OnDrawGizmos() {
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(Isometric.isoProjection(obj.Position), .1f);
}
}
Heres the controller
using UnityEngine;
using System.Collections;
public class NodeIsoObjectController : AbstractIsoObjectController {
//units/second
public float speed = 2f;
public int movement = 5;
public bool loop = true;
public Node[] nodes;
public int nextNode = 0;
void Start() {
this.isoObj = GetComponent<IsoObject>();
moveToNextNode();
}
public void moveToNextNode() {
if (nodes.Length > 0 && nodes[nextNode] != null) {
var newPos = nodes[nextNode].Position;
float distance = (isoObj.Position - newPos).magnitude;
moveTo(newPos, (x) => EasingFunctions.Linear(x, 0, 1, distance / speed), () => {
if (nextNode < nodes.Length - 1) {
nextNode++;
moveToNextNode();
} else {
if (loop) {
nextNode = 0;
moveToNextNode();
}
}
}, 0, distance / speed);
}
}
Answer by codebeans · Jun 22, 2015 at 06:56 AM
Hi, since the original question referred to something like a pedestrian system such as in "Roller coaster tycoon", I will try to give you some input on that. This is not specific to my Asset but rather an overall explanation.
The reason is I have a sprite that will walk around and wander on its own and will walk from one tile to the nearest (depending on the distance cost of how far they are willing to go). Think of it like Rollercoaster tycoon where each walkway has a node and if there is a fork in the road the people will select randomly between the two and proceed to move to the nearest next tile.
A very simplified version of your required behaviour could be implemented like this:
Have a overall node system, something that stores all nodes or holds references to them. Use a singleton combined with a observer pattern to get all references of Node instances easily . (Just so that you can access all nodes without polling GetComponents or something with big overhead).
Now we assume you have a player in your scene which has a position (doesn't matter if we are talking about IsoObject.Position or transform.postion or..) as a Vector3. The Nodes do have a position as Vector3 as well. So when your player has to decide its next node to go to just loop over all nodes an calculate the distance via Vector3.Distance(playerPosition,nodePosition). Then find the minimum. You could chop the maximum distance by your
range x number of tiles
And just move to that node. This would probably lead to the problem that your player doesn't move at all since the minimum distance to the next node might be the one he is standing on right now, or even worse the one he came from. So he would go back and forth between the two closest nodes.
So make sure to exclude the current and last node from your calculations.
This would be a very simple pedestrian system. It's performance would be pretty bad and I wouldn't recommend such an implementation for a final product. But as a proof of concept or some rapid prototype this might be suitable.