- Home /
How do I single out one object amongst a group of objects with the same tag?
This question continues from my last post. I'm making a game where you're a zombie, and you, along with your zombie follower guy, have to infect every human. So far, I've gotten any zombies that are created to follow me around, and chase any humans that get close to them. This is my Zombie script (attached to my zombie follower):
var leader : Transform;
var speed : float = 1;
var humans : GameObject[];
var chasing = false;
var zombieFollower : Transform;
var picked = false;
function Start () {
}
function Update () {
humans = GameObject.FindGameObjectsWithTag("human");
for (var human : GameObject in humans){
var walkTo = Vector3(leader.position.x, 0, leader.position.z + 10);
var thisZombie : GameObject = this.gameObject;
var range : float = Vector3.Distance (thisZombie.transform.position, human.transform.position);
if (range < 10){
chasing = true;
var position : Vector3 = Vector3(human.transform.position.x, 0, human.transform.position.z);
thisZombie.transform.LookAt(position);
thisZombie.transform.Translate(speed * Vector3.forward * Time.deltaTime);
if (range < 2){
var spawnPosition : Vector3 = Vector3(human.transform.position.x, human.transform.position.y, human.transform.position.z);
Destroy(human);
var whatToclone = Instantiate(zombieFollower, spawnPosition, Quaternion.identity);
chasing = false;
}
} else {
chasing = false;
thisZombie.transform.LookAt(walkTo);
}
}
}
The zombie gets confused when two humans come within range. How do I single out one human for the zombie to go after?
Create a target variable, create a target array, that is filled with all humans in range. Then, set the target variable to a random object from the target array. There you go, you're set to one human.
Answer by Seizure · Aug 01, 2013 at 04:07 PM
Try this:
var leader : Transform;
var speed : float = 1;
var humans : GameObject[];
var chasing = false;
var zombieFollower : Transform;
var picked = false;
function Start () {
}
function Update () {
humans = GameObject.FindGameObjectsWithTag("human");
for (var human : GameObject in humans){
var walkTo = Vector3(leader.position.x, 0, leader.position.z + 10);
var thisZombie : GameObject = this.gameObject;
var range : float = Vector3.Distance (thisZombie.transform.position, human.transform.position);
if (range < 10 && !chasing){
chasing = true;
var position : Vector3 = Vector3(human.transform.position.x, 0, human.transform.position.z);
thisZombie.transform.LookAt(position);
thisZombie.transform.Translate(speed * Vector3.forward * Time.deltaTime);
if (range < 2){
var spawnPosition : Vector3 = Vector3(human.transform.position.x, human.transform.position.y, human.transform.position.z);
Destroy(human);
var whatToclone = Instantiate(zombieFollower, spawnPosition, Quaternion.identity);
chasing = false;
}
} else {
chasing = false;
thisZombie.transform.LookAt(walkTo);
}
}
}
I also need the zombie to target a human that is within range. Any ideas?
Would it involve storing the current human that is being chased in a variable?
I'll be gone for the next week.
Still answer, please, I'll read them when I get back.
Thx
Answer by jacobschellenberg · Aug 01, 2013 at 04:07 PM
You could add a property to the Humans called ID or something of the sort that uniquely identifies each Human.
Answer by nixcs2512 · Aug 01, 2013 at 09:25 PM
You can find closest human each frame but actually it won't work in some cases. In example we have 2 human,A and B, human A is closer to the zombie than B, but A is running away and B is coming to the zombie, the strange thing happened : zombie will chase B instead of A. If B runs and A comes, that happened again. And with a little trick, zombie will like chasing no one. So think again about how it would work.I will say that: we have a boolean variable called locktarget, if(!locktarget) zombie will start moving randomly around and searching for human within range,if it can found someone,say the closest target, it will choose that target as currenttarget and keep chasing it, until the distance between them get over range.
bool locktarget;
GameObject[] humans;
GameObject currenttarget;
float range;// the range to detect human
void Update()
{
humans = GameObject.FindGameObjectsWithTag("human");
if(!locktarget)
{
//Moving randomly or follow the leader
float closestrange=Mathf.Infinity;
GameObject closestObject;
for(GameObject human in humans)
{
float distance = Vector3.Distance(transform.position,human.transform.position);
if(distance<=closestrange)
{
closestrange = distance;
closestObject = human;
}
}
if(closestrange<=range)
{
locktarget=true;
currenttarget = closestObject;
}
}
else //while locking human
{
//chase him
float distance = Vector3.Distance(transform.position,currenttarget.transform.position);
if(distance>range)
{
locktarget = false;
currenttarget = null;
}
}
}
You need to finish yourself the chasing code and following leader,etc..And by the way sorry for C# code but i only know C#.Actually this code is really easy to understand , at least when you can code above thing. Too confusing with me.