- Home /
Pick a Transform[] or GameObject[] from script?
Hi.
I wanted to improve my enemy AI by adding waypoints to it, so it would look a bit more realistic. I've got everything figured out, except for setting a random Transform[] or GameObject[] from a variable for them to go to. Is it possible, or do I need to find another way?
EDIT: Does not need to be random. Just a way to pick them one at a time.
Answer by rutter · May 15, 2012 at 06:01 PM
If we assume you have a list or array of items to choose from, you can use Random.Range() to pick an int between 0 and the length of your list, and use the Xth list item.
O$$anonymous$$, thanks! I got the randomization ready, but I don't know how to get it's position. It's just a number.
Here is what I did:
NavComponent.SetDestination(Random.Range(0, wayPoints.Length).position);
NavComponent is a variable that deter$$anonymous$$es which Nav$$anonymous$$eshAgent to use.
Answer by Bunny83 · May 15, 2012 at 06:22 PM
For something like that i would use a custom data class like this:
public class AIPath
{
var waypoints : Transform[];
}
In your actual script you can define a public variable like this:
var paths : AIPath[];
private var currentPath : AIPath;
Now you can create multiple seperate pathes. To select a path, just use Random.Range like rutter said.
currentPath = paths[Random.Range(0,paths.Length)];
Then just use the waypoint array from the currentPath.
currentPath.waypoints[...]
Your answer
Follow this Question
Related Questions
Generating a Displacement Map in Unity3d? 1 Answer
NaN exception when spawning soldiers 1 Answer
Finding Children question 3 Answers
How to Change Background Sprite at run time ? 0 Answers
Control Ship and Gun Separately 2 Answers