- Home /
Calling GameObject's transform from list
Hello! I am trying to call a target from a list. I am trying to reference the transform.position for the gameObject I am calling (closestPlayer) so I can "MoveTowards" it. How do I do this? I've been looking at this yesterday and today getting addingly smarter (and frustrated), but I can't seem to get it to work. Everything else works fine, though! :) Relevant code:
public class EnemyAI : MonoBehaviour {
private GameObject closestPlayer;
public float speed;
public float maxDistance;
public GameObject FindClosestPlayer() {
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Player");
maxDistance = 300f;
Vector3 position = transform.position;
foreach (GameObject currentPlayer in gos) {
Vector3 diff = currentPlayer.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < maxDistance) {
closestPlayer = currentPlayer;
maxDistance = curDistance;
}
}
return closestPlayer;
}
void Update () {
GameObject closestPlayer = FindClosestPlayer();
// transform.position = Vector2.MoveTowards(the closestPlayer <--
As you can see for the last line of code, I want to reference the "closestPlayer" correctly, so my EnemyAI can move towards it! I appreciate any help, thank you.
Wisearn, the OP is right to use sqr$$anonymous$$agnitude... Sqr$$anonymous$$agnitude is used becuase it dodges a lengthy Square root function that computers just hate it( magnitude is equal to the square root of the sum of each axis difference)... in short its a performance thing
you want to sort out the closest player right? basically sort by distance calculations...
http://answers.unity3d.com/questions/341065/sort-a-list-of-gameobjects-by-distance.html http://answers.unity3d.com/questions/246781/sort-transforms-by-distance-to-player.html
give these a read... if you still having trouble let me know
you last line of code would be
transform.position = Vector2.$$anonymous$$oveToward(transform.position, closestPlayer.transform.position,maxDistance)
http://docs.unity3d.com/ScriptReference/Vector2.$$anonymous$$oveTowards.html (this ones a little vague, so here a link to Vector2.Lerp as well) http://docs.unity3d.com/ScriptReference/Vector2.Lerp.html
I played around with some code, this seemed to work fine. What do you guys think, any flaws?
closestPlayer.transform.Translate (0, 0, 0);
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, closestPlayer.transform.position, speed * Time.deltaTime);
No comments as answers please
The first line of your code in this comment is useless and can be deleted. The second line looks good.
OP is right to use sqr$$anonymous$$agnitude if you are comparing one sqr$$anonymous$$agnitude to another sqr$$anonymous$$agnitude, but sqr$$anonymous$$agnitude will not represent an actual distance (300f "maxDistance" would be something like 17 units and not 300 units in your scene).
In the code from the unity scripting reference they use $$anonymous$$athf.Infinity as a "maxDistance" which makes sense because it's not a real number (unlike 300).
But yes sorry you don't need to use $$anonymous$$ath.Abs since magnitude already does this.
I'm sorry, I thought to myself that it could be a potential answer to my own question (which it sort of was), that is why I didn't use it as a comment. But, I will consider not doing so in the future, thanks for the notice! Thank you for the comment, btw. Cheers
All good. I just reread your answer. I was probably a little harsh on my moderation. Sorry about that.
Answer by Kiwasi · Oct 15, 2014 at 08:10 AM
You could simply use
closestPlayer.transform.postion;
Its better to work with transforms directly if you are mostly interested in position. Change the return type of FindClosestPlayer to a transform. Then you could use
closestPlayer.position;
Your answer
Follow this Question
Related Questions
What is the best way to instatiate an array of connected GameObjects? 0 Answers
How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers
Subtracting the position of transform from the position of Game Objects in a list. 1 Answer
How to select an Game Object from an Item List 0 Answers
How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method? 5 Answers