- Home /
Multiple targeting help
Currently I have a script attached to my enemy characters that tries to detect where the player is. it first uses a view cone as a trigger. if the cone is triggered, it checks with a line cast to see if there are any obstacles.
If both checks succeed, the enemy will now follow the player. using:
AIFollow.target=GameObject.Find("PlayerModel").transform;
Debug.Log("current target is " + AIFollow.target);
chasing = true; display.Chasing();
However, right now. if one enemy see's the player; ALL the enemies will follow it.
It's pretty annoying, and I'm not sure how to fix the issue. If anyone has any ideas, please let me know, I'm happy to show any other code I'm using if it helps fix it.
also, as a bonus, what would be the best way to get the enemy to walk around randomly until the player arrives?
Thanks.
Are you setting target from a global standpoint? Seems like you would set it on the enemy that spots it only. Like
if(spottedEnemy)Chase();
or something of the sort, that way only enemies that spot the player will actually follow it. It is tough to tell w.o knowing what these other references are too.. $$anonymous$$y guess is there is only one instance of the AI script and all enemies reference that single script(in which case this is intended behavior).
yes you are absolutely correct. The 3 scripts governing the targeting and following are attached to, and children of the EnemyObject. I am then instancing the EnemyObject onto the level.
If it helps, I am assigning them all individual random 'gameObject.name' variables on awake.
Answer by hijinxbassist · Jun 26, 2012 at 08:18 AM
Each enemy should have its own script to govern its actions (this makes it 'think' independently from its brethren). You can instead assign that script to the new instance using
instance.gameObject.AddComponent(AIScript);
or just add the ai script to the enemy prefab(more sensible in this case). You may need to rewrite some stuff. Remember, if you need to access that newly instantiated enemy, you can use a ref var in front of the instantiation call
var newEnemy=Instantiate(enemy,Vector3.zero,Quaternion.identity);
var aiScript=newEnemy.GetComponent(AIscript);
//aiScript.player=playerVariable; //or something of the sorts
I see! That's awesome! So I would have the current script in the project library but NOT attached to the objects. I would then place instances of my enemies, with scripts attached that simply say 'instance.gameObject.AddComponent(AIScript);' ins$$anonymous$$d of actually writing the whole script like it is currently. is this correct?
I would make an AI script and attach it to the enemy prefab. You can now instantiate the prefab(the script will already be attached to the new enemy), then you can pass info to it using the ref variable. Then they will 'think' on their own. In my example the last line(the blocked line) i pass a reference of the player to the enemy so it knows who to attack if the conditions are met. If the script you are creating enemies from is on the player itself, you can simply say something like
newEnemy.GetComponent(AIScript).player=gameObject;//or transform
Your answer
Follow this Question
Related Questions
Disable a target after trigger exit? 1 Answer
drone to target enemies 2 Answers
Mouse lock to target 0 Answers
Problems with raycast obstacle avoidance 1 Answer
Raycast hits a collider and halts. 1 Answer