Detect if an object is next to another object or finding direction of it ( I wrote a script)

I just wrote a script for snapping two object together, firstly function should find the direction of obj[1](object that should stick to parent object), I want the function (detect) returns only one result (single direction), but sometimes returns two direction for example : at the same time returns : left and down here is my question what is the main problem of my script how can I fix it ? most of the time can’t detect top btw

Thank you.

 public    Transform [] obj = new Transform[2];

     public    bool run = false;

     float distance = 5.0f;

     public Pipe_in p_in;

     public    int direction = -1;

public void Detect_Direction ()

   {

     if (p_in.loop_move_cycle_of_Pipes_in == false)

     {


       obj [0] = GameObject.Find (p_in.Switch_B.name).GetComponent<return_pipe_name> ().Pipe;

       obj [1] = p_in.Switch_B;


       Vector3 offset = obj [0].position - obj [1].position;

       offset = offset.normalized;

       Vector3 up = obj [1].InverseTransformDirection (Vector3.up).normalized;

       Vector3 down = obj [1].InverseTransformDirection (Vector3.down).normalized;

       Vector3 left = obj [1].InverseTransformDirection (Vector3.left).normalized;

       Vector3 right = obj [1].InverseTransformDirection (Vector3.right).normalized;


     Debug.Log ( "Up: " + Vector3.Dot(up, offset).ToString() +
              " Down: " +  Vector3.Dot(down, offset).ToString() +
              " Right: " +  Vector3.Dot(offset, right).ToString() +
              " Left: " +  Vector3.Dot(left, offset).ToString());



       if (Vector3.Dot (up, offset) > 1) // before: > 0

       {
         print ("obj[1] : Up");
         direction = 0;
       }

       if (Vector3.Dot (down, offset) < Vector3.Dot (left, offset)) // before: < 0

       {
         print ("obj[1] : Down ");
         direction = 1;
       }

       if (Vector3.Dot (right, offset) > 1) // before: > 0

       {
         print ("obj[1] : Right");
         direction = 2;
       }

       if (Vector3.Dot (left, offset) < Vector3.Dot (down, offset)) // before: < 0

       {
         print ("obj[1] : left");
         direction = 3;
       }


       float mag = offset.sqrMagnitude;

       if (mag < distance * distance)

       {

         obj [1].transform.position.Normalize ();

         obj [1].transform.parent = obj [0].transform;

         if(direction == 0)
           obj [1].transform.localPosition = Vector3.up;

         if(direction == 1)
           obj [1].transform.localPosition = Vector3.down;

         if(direction == 2)
           obj [1].transform.localPosition = Vector3.right;

         if(direction == 3)
           obj [1].transform.localPosition = Vector3.left;


       }


       if (direction != -1)
         p_in.Switch_B = GameObject.Find ("x").transform;


     }

   }

Not tested this code but I hope it will return one main direction (Stole it from a java post and converted it).
Should work from your offset vector.

    Vector3[] compass = new[] { new Vector3(1f, 0f, 0f), new Vector3(-1f, 0f, 0f), new Vector3(0f, 1f, 0f), new Vector3(0f, -1f, 0f) };

    public Vector3 getDir(Vector3 v)
    {
        float maxDot = -Mathf.Infinity;
        Vector3 ret = Vector3.zero;
        foreach (Vector3 dir in compass)
        {
            float t = Vector3.Dot(v, dir);
            if (t > maxDot)
            {
                ret = dir;
                maxDot = t;
            }
        }
        return ret;
    }
1 Like

You should then be able to use the return vector directly or you can do
if returnVector.x=1 then dir=up
if returnVector.x=-1 then dir=down
if returnVector.y=1 then dir=right
if returnVector.y=-1 then dir=left

2 Likes

hello sir
great, thank you so much it works. you made my day :slight_smile:

1 Like