Search for Object with largest y-Coordinates
Hello there! I want to search for an Object in Objects with the same Tag who has the highes / largestt y-coordinates in the scene. I know i could do it with put the objects when they spawn in an array and then ckeck with loops the highest position, but may there is a better way and smoother way. I need this because my Camera needs to go move up on the y-coordinates if the objects pile and become more and more higher, other hints for my solutions would be great aswell. :)
Ty for reading and coming answers, excuse my bad english.
Answer by jgodfrey · Mar 17, 2016 at 10:39 PM
Well, something is going to have to iterate through the objects in question and find the one with the largest Y value. You should be able to find the objects in question using something like:
GameObject[] gos = GameObject.FindGameObjectsWithTag("yourTag");
Then, just spin through the objects in the returned array and find the one you're interested in.
Alternatively, you could probably use some LINQ to get what you want. Caution - typed directly into the forum and completely untested...
GameObject go = GameObject.FindGameObjectsWithTag("yourTag")
.OrderByDescending(go => go.transform.position.y).First();
If you choose to use some LINQ, you'll need to add a "using System.Linq;" to the top of your file.
Yeah i got this aswell 10$$anonymous$$ after i opened this thread, but my problem is now that i dindt get how i can move my camera and cameraobject smoothly if an object become higher y-coord. in my code it just instantly changes the position and its not good cause the physics are not working if it jumps so fast between the positions.
public Vector3 Hoehstgelegen;
public GameObject[] Liste { get; private set; }
public GameObject Boden;
...
...
...
Liste = GameObject.FindGameObjectsWithTag("Objekt1").OrderBy(liste => -liste.transform.position.y).ToArray();
Hoehstgelegen = new Vector3(Liste[0].transform.position.x, Liste[0].transform.position.y, 0);
float distanz;
distanz = Vector3.Distance(Boden.transform.position, Hoehstgelegen);
transform.position = new Vector3(transform.position.x,4+distanz,transform.position.z);
got it!
float distanz;
distanz = Vector3.Distance(Boden.transform.position, Hoehstgelegen);
Vector3 testen;
testen = new Vector3(transform.position.x, 4 + distanz, transform.position.z);
transform.position = Vector3.$$anonymous$$oveTowards(
transform.position,
testen,
Time.deltaTime);
}
Do yourself a favor and open a new thread as you now have a completely different question.
Your answer
Follow this Question
Related Questions
Show Text where user clicks in a panel,Show text where User clicks 0 Answers
Toogle Parent to Camera Function - Google Cardboard 0 Answers
When playing builded version of game the cameras only captures part of scene. 0 Answers
Zoom camera 1 Answer
Camera Jitters When Displacing and Rotating Smoothly 0 Answers