- Home /
Way Point Assignment
This topic has been asked a couple of times, but none of them i understand very well. The problem is i want to spawn my enemies, then have them follow way points. But sense i can't assign them in the project panel i have to do i by script. i will post the script that i am using (JavaScript). thanks!
var waypoint : Transform[];
var speed : float = 5;
private var currentWaypoint : int;
function Update ()
{
if(currentWaypoint < waypoint.length)
{
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1)
{
currentWaypoint++;
}
else
{
velocity = moveDirection.normalized*speed;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}
Answer by Brett_MMU · Aug 10, 2012 at 06:10 PM
If you need them in order then you'll have to do something to each waypoint to make it identifiable. If you don't have many waypoints the easiest thing to do would be to tag each waypoint as waypoint1, waypoint2, waypoint3 etc, then have
var waypoint: GameObject[];
Start()
{
waypoint = new GameObject[numberOfWaypoints];
waypoint[0] = GameObject.FindGameObjectWithTag("waypoint1);
waypoint[1] = GameObject.FindGameObjectWithTag("waypoint2);
waypoint[2] = GameObject.FindGameObjectWithTag("waypoint3);
}
or you could attach a script to each waypoint with a public int set to it's ID;
then have (this is in C#, not sure if it's the same for JavaScript)
GameObject[] waypoints;
GameObject[] sortedWaypoints;
Start()
{
waypoints = GameObject.FindGameObjectsWithTag("waypointTag");
sortedWaypoints = new GameObject[waypoints.Length()];
foreach (GameObject g in waypoints)
{
sortedWaypoints[g.GetComponent<waypointScript>().ID] = g;
}
}
if you did it this way you're first waypoint must have an ID of 0, then the second waypoint has an ID of 1 and so on
that I can think of, can you not select the script in the project pane and drag the waypoint transforms into the inspector?
i can do that i just wanted to explore my options a little. :)
I tried the second way (it would be much easier because i have 17 waypoints), and i don't think it works for javascript. i got 6 errors. :(
Answer by Brett_MMU · Aug 10, 2012 at 02:04 AM
what I do is tag my waypoints, then add a start function like this (it's in C# but I imagine it's the same for JavaScript)
Start()
{
waypoints = GameObject.FindGameObjectsWithTag("waypointTag");
}
If you do it this way though you'll have to change waypoints to a GameObject instead of a transform, and then use waypoint[currentwaypoint].transform.position
where would i add the waypoint[current waypoint].transform.position?
replace
var target : Vector3 = waypoint[currentWaypoint].position;
with
var target : Vector3 = waypoint[currentWaypoint].transform.position;
alight i did what you said, and it works more reliably now, but when it finds the gameobjects with the tag waypoint, it puts them in a random order inside the array.
Answer by Brett_MMU · Aug 10, 2012 at 06:50 PM
var waypoints : GameObject[];
var sortedWaypoints : GameObject[];
function Start()
{
waypoints = GameObject.FindGameObjectsWithTag("waypointTag");
sortedWaypoints = new GameObject[waypoints.length];
for (g in waypoints)
{
sortedWaypoints[g.GetComponent(waypointScript).ID] = g;
}
}
try that
okay no more errors, but what do you mean bu id in the first answer? is that the name or something else? i named all of the waypoints 0-16 in case that is what you meant.
create another javascritpt called waypointScript and just add
public var ID : int;
then attach it to every waypoint, and set the ID in the inspector
i got a null referance at this line:
sortedWaypoint = new GameObject[Waypoint.Length];
does your waypoint array have an upper case W? if not try it with a lower case w
sortedWaypoint = new GameObject[waypoint.length];
okay i did lowercase that got rid of it, but they still don't follow the way points in order of the id i gave them on the script.
Answer by Brett_MMU · Aug 10, 2012 at 08:01 PM
Add
Debug.Log(currentWaypoint); Debug.Log(sortedWaypoint[0].transform.position);
after that line and tell me what you get
sorry for the latness but i got 2 things:
UnityEngine.Debug:Log(Object)
2.(36.7, 2.5, 48.6)UnityEngine.Debug:Log(Object)
the only thing I can think of that would be throwing a null reference exception is that your ID's aren't correct, a typo maybe? like 0,1,3,3,4 or something like that
is it constantly been thrown?
it works now. for some reason the ids of all the way points reverted to 0 even though i set them. :) Thanks a million!
Your answer
Follow this Question
Related Questions
What is wrong with this code? 0 Answers
add waypoint to transform[] 2 Answers
Enemies Spawn, Then Follow Waypoints 1 Answer
problem extracting transform from an array 1 Answer
Spawn "n" Enemies Every "nth" Wave? 1 Answer