1
\$\begingroup\$

I am making a puzzle game in unity3D 5 for school and one of the puzzles is this: The player (third person) can pick up certain objects (4 objects) and has to place them inside holes in a wall in a specific order for a door to open.

I have 4 empty gameobjects named as slot001, slot002, slot003 and slot004 at the right positions at the holes in the wall. When the player has an item held and a certain button is pressed I link the transform of the object to the transform of the empty gameobject.

The problem is that the objects transform has to link to the transform of the closest gameobject. So when you stand in front of the slot002 gameobject the objects transform should link to the transform of slot002. The same goes for slot 001, 003 and 004 ofcourse. I am not sure how to do this.

I put the 4 empty gameobjects (slots) in a list. I know of the Vector3.Distance() function and I tried using that to check for the closest distance with Mathf.Min() but I couldn't quite get it to work.

Any help or a nudge in the right direction would be appreciated!

Thanks.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$
GameObject closestObject = null;
var distance = SnapDistance; // guarantees it will only snap once close enough.  Assuming some value that is inspector editable, like 3.
// if you don't want a snap distance, then set the starting distance to float.MaxValue
foreach(var slot in ListOfSlots) // ListOfSlots is GameObject[] or List<GameObject>
{
   // gets the current distance from this object to the slot.
   var currentSlotDistance = Vector3.distance(slot.transform.position, transform.position);

   // check the distance.  if this is closer than the previous distance, then this is our closest object.  By checking each object to see if it is closer than the last, we will eventually find the closest one.
   if (currentSlotDistance < distance)
   {
     // once found, set the distance to our current distance, and set the closestObject to the slot we are looking at.
     distance = currentSlotDistance;
     closestObject = slot;
   }
}

if (closestObject != null) // we have a winner  closest under X distance
{
  // assuming you wanted to move this object into the slot:
  transform.position = closestObject.transform.position;
}
\$\endgroup\$
5
  • \$\begingroup\$ Thanks for your response first of all! I think I get where you're going with this. Could you maybe convert it to c# though? I have never learnt javascript :) \$\endgroup\$
    – S. Neut
    Mar 16, 2017 at 12:34
  • \$\begingroup\$ This looks like C# to me already. Did I miss an edit? If not, S. Neut, can you please clarify which parts might be giving you trouble? \$\endgroup\$
    – DMGregory
    Mar 16, 2017 at 16:24
  • \$\begingroup\$ Hmm I'm not sure where in my code to place this piece of code. I forgot to link my code anyway, that was not handy of me :) Here is the code that I have now: pastebin.com/zs1S8Prq \$\endgroup\$
    – S. Neut
    Mar 16, 2017 at 17:05
  • \$\begingroup\$ @S.Neut It'll probably go somewhere near //Place the object in the right slot. But this isn't a copy/paste solution. Your job as a developer is to take the algorithm presented above and implement it into your code. \$\endgroup\$
    – House
    Mar 16, 2017 at 21:59
  • \$\begingroup\$ @Byte56 You are right, I will try my best to make it work and let you know if I did it or not. \$\endgroup\$
    – S. Neut
    Mar 16, 2017 at 22:58
0
\$\begingroup\$

So I almost got it to work thanks to the answer of Dan Violet Sagmiller, there is just one small problem left. I need to check if a slot is already occupied. How could I do this? I was thinking of an array of booleans but that didn't quite work.

Here is the current code that I have now: http://pastebin.com/XYQ4hBEX

\$\endgroup\$
1
  • \$\begingroup\$ You can add the object as a child of the slot. And when determining which slot is closest you rule out those with children. Helpful hint: the children of an object can be found (oddly enough) under transform.children \$\endgroup\$
    – lilotop
    Mar 18, 2017 at 19:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .