- Home /
One player tag, two players, enemy attack who ever is closer?
I'm thinking about how to go about this. Any thoughts?
You could have a little algorithm that deter$$anonymous$$es it based on a few different factors; like player's health levels, or the aggregate of health and armor. You could invert that functionality based on difficulty level, so that the enemies go after the stronger of the two on easier levels.
Just some thoughts...
I was thinking more along the lines of whoever is closer. The better player shouldnt be punished for being better lol. I gues what I'm asking is how to deter$$anonymous$$e which game object is closer to the enemey object.
If you already know the position of the two players and the enemy you can do something like:
dist1 = Vector3.Distance(player1.transform.position, enemy.transform.position);
dist2 = Vector3.Distance(player2.transform.position, enemy.transform.position);
if (dist1 < dist)...
If you are asking how an enemy finds the players, there are different ways depending on the structure of the game. Start with Physics.SphereCastAll().
I'd like to stick with what I have already, since it almost works. I'm sure I'm just overlooking something. Unless the 2nd method you mentioned has noticeably better performance.
Oh, well your question to me seemed phrased like you hadn't made up your $$anonymous$$d about who the enemy was going to attack and wanted input.
Since that is not the case, robertbu's answer is correct.
If you concerned with performant code, and didn't want to use Vector3.Distance(), then you could also do:
float
d1 = (transform.position - gameObject1.transform.position).sqr$$anonymous$$agnitude,
d2 = (transform.position - gameObject2.transform.position).sqr$$anonymous$$agnitude;
if (d1 < d2)...
http://docs.unity3d.com/Documentation/ScriptReference/Vector3-sqr$$anonymous$$agnitude.html
Answer by bpears · Jan 26, 2013 at 08:11 PM
The way I had the script set up in the beginning was working how it should.
It actually ended up being a problem with one of my gameobjects transform position being off from import somehow, So I instead assigned an empty game object in its place, which has accurate transform position.
If anyone knows why imported models have messed up positions, and how to make them accurate to unity, and keep their current placement in unity, leave a comment please.
I don't think there is a way to modify a mesh's origin from within Unity; it needs to be edited externally so that it's at the 0,0,0 (or preferred) position.
Also, a tip to debug this problem:
In update:
put Debug.Log("distance1stTarget "+distance1var+" distance2ndTarget " +distance2var)
This will update how far away the enemy is from each so you can watch in console. Put your targets in the same spot, if the distances are not equal(or at least pretty close) when you play, you have the same problem I did.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
AI Follow Player "Tag" Help 1 Answer
EnemyAI Script help 1 Answer
Distance thing with gameobject ? 4 Answers
Make player not be seen by AI, when player in foilage and shadows. 1 Answer