Hi, I’ve been searching for results on way point navigation but I can’t seem to find anything that can help me. Below is a picture of my terrain.
I have a terrain, a minimap and a First Person Controller (Red dot). What I’m trying to figure out is that when i click those (Blue dots) on the minimap, the first person controller (Red dot) will slowly walk to where the (Blue dot) that I’ve clicked is. It must also walk along the path on the terrain. Is there any way i could do that?
The way that I’m trying to do is to store the position of the Green dots. And later on passing it to the FPC when the blue dot is clicked, so the red dot will go to the blue dot through those green dots.
I tried searching the forum on how to begin. Tried quite a number of codes, but all don’t seem to work. ):
Both of the codes below allows me to move to the 1st waypoint, and it just stops there. I need it to complete it’s route through all waypoints.
This is what I’ve tried coming up with.
var points : GameObject;
var points1 : GameObject;
var points2 : GameObject;
var continuous = false;
function Update () {
if(Input.GetKeyDown(KeyCode.I) || continuous){
transform.position.x = Mathf.Lerp(transform.position.x, points.transform.position.x, Time.time/45);
transform.position.z = Mathf.Lerp(transform.position.z, points.transform.position.z, Time.time/45);
continuous = true;
if (transform.position.z == points.transform.position.z) {
transform.position.x = Mathf.Lerp(transform.position.x, points1.transform.position.x, Time.time/80);
transform.position.z = Mathf.Lerp(transform.position.z, points1.transform.position.z, Time.time/80);
continuous = true;
}
}
}
This is something i took from some forumers and edited a bit of it.
var points : GameObject[];
var closest : Transform;
var minDist=10;
var closestIndex: int = -1;
var continuous = false;
function FixedUpdate () {
if (Input.GetKeyDown(KeyCode.I) || continuous) {
for (i = 0; i < points.Length; i++) {
if ((closestIndex != -1) (i != closestIndex))
var dist = Vector3.Distance(transform.position, points[i].transform.position);
if(dist<minDist){
closestIndex=i;
closest=points[i].transform;
transform.position.x = Mathf.Lerp(transform.position.x,closest.position.x, Time.time/45);
transform.position.z = Mathf.Lerp(transform.position.z,closest.position.z, Time.time/45);
continuous = true;
}
}
}
}
I cant remember What the name of the AI script is but lets pretend its RobotAI. I would either do this:
var myAI: RobotAI;
function Update(){
if (Input.GetKeyDown(KeyCode.I)){
myAI.enabled=true;
}
}
and have RobotAI disabled (and assigned to the correct slot) before you start the scene. Alternatively you could build it into the same script, but I’d have to open the project and work that out. try this “separate script that enables AI script” approach first, in the interests of programmer laziness.
Thanks! Managed to solve that. But now when I attached a camera to the GO, it’s rather shaky and jerky and the camera goes crazy once in a awhile while moving towards the way points. Any idea why does this happens? I tried attaching the main camera in your game to the robot and it’s working fine. But no idea why it’s not working for mine. ):
Thanks for the constant help Aaron. (: It really helped me to get started with what I’m doing. But just another question. Now the waypoints has this “isStart” thing whereby the GO will start following the waypoints from the waypoint which is assigned with the “Is Start”. Any idea how can I make the GO follow the waypoints but need not start from the 1st waypoint? But rather start from the closest waypoint.
Below are part of the script which I think affects what I’m aiming to do.
DirectionalWaypoint Script
static var start : DirectionalWaypointBlue;
var next : DirectionalWaypointBlue;
var isStart = false;
function Awake () {
if (isStart)
start = this;
}
AI2 Script
function Start () {
// Auto setup player as target through tags
if (target == null GameObject.FindWithTag("Player"))
target = GameObject.FindWithTag("Player").transform;
activeWaypoint = DirectionalWaypointBlue.start;
Patrol();
}
look just hacking this together, is untested (mostly from FindObjectsWithTag script ref page). Tag all Waypoints as “WP” for purposes of this demo:
function Start(){
print(FindClosestEnemy().name);
FindClosestEnemy();
var startPoint=FindClosestEnemy().GetComponent(DirectionalWayPoint);
startPoint.isStart=true;
}
// Find the name of the closest enemy
function FindClosestEnemy () : GameObject {
// Find all game objects with tag WP
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("WP");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
That is an untested guess and may be quite wrong!
I have a bit on but if you want to hire me specifically for half an hour ($20) sometime thats something that could help us both.
I have a detailed post on my blog however its a little too much for your requirements www.planet44.wordpress.com
However, what you really want to think about is A) set up an array to store all your waypoints in with their relative positions and B) then do the movement
Its really great to do these 2 things, even if you dont absolutely have to. Calculation of a list of waypoints can be all done within Awake so it only runs once at the start of the game.
Essentially you are saying, at the start of the game lets get all the waypoint positions(can be done with overlap sphere radius of your map) into an array, then you can also calculate their neighbours into a second list
You asked how to get all the positions?
To store the position the code structure looks something like this
First set all eg orange nodes to a new layer called orange on eg layer 8
//get array of all orange nodes
myArray = OverlapSphere ((0,0,0), 9999999999, 8); //from the center of map, 9999999999 radius, orange
//return each one
locationwaypoint1 = myArray[1].transform.position
locationwaypoint2 = myArray[2].transform.position
etc
also you can return names with myArray[1].transform.name