- Home /
How do you find one Vector's position of an Array?
Hello,
Forgive me if this has already been asked, but I couldn't find anything.
I have an array of Vector2's and at each one of those Vector positions there may or may not be a game object. The idea is that once one of these game objects is hit with RaycastHit I want to compare it's transform position with the list of Vector2's in the array. When I return a match, then I've done exactly what I want to do, but the only way that I,ve been able to do that so far is manually writing:
if( hitGameObject == myVectorTwoArray[ 3,4 ]; )
I'm not overly keen on writing down each array, not just because of the time it takes, but also because my scenes will vary in size and some will need a larger list of arrays. So, I would have to rewrite the code for every scene.
Does anyone know how to do this? Or is it even possible?
I apologize if this is something simple.. I have just over a month of programming exp.... Total Noob!!
Thanks in advance.
Answer by robertbu · Feb 23, 2013 at 06:49 AM
I'm visualizing a minefield here. Comparing Vector2 is problematic since they are composed of floats. One way to solve this issue is to push information down to the game objects. Record the row/col information in a script attached to each game object. When you get a hit, you can immediately find the array entry.
I'm not sure what information you are storing at the array positions, but you might push that down to the game objects as well so during a hit you don't have to refer to the array at all.
When you say "writing down each array," I thinking that you are doing a comparison with hard-coded values like you have in your question. If you don't already know, nested for loops will solve this more simply.
Thanks for the advice robertu.
I'll try out your suggestions.
I created the array with nested loops, I just meant that I would have to write out an if-statement for comparing every array location with the game object's transform position.
Anyways, since you mentioned the nested loop, you got me thinking that I can accomplish what I need by storing the array position in the game objects like you said while the nested for loop is doing it's thing.
No $$anonymous$$efield, but it sure sounds like it hey!!!
Thanks again!
Your answer
Follow this Question
Related Questions
How do I find the position of an element in an array? 3 Answers
Unity3D - Playback object array of position (with dynamic velocity) 0 Answers
How do I check the distance between multiples objects in an array from the player? 4 Answers
How do I instantiate same tag/name GameObjects with an array 1 Answer
How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers