Want Object A to send a message to Object B when it is directly above it

I have two objects: a tile and a block that are the same size. The block is constantly moving forward and has built in functions to change its moving direction. Now I have this tile that has the ability to send a message to the block to change its direction.

What I want is the block to receive the send message precisely as it is passes over the tile. Solutions I have tried:

OnTriggerEnter - the problem here is that the block enter the trigger of the tile at the beginning and is offset

RayCast - the raycast initially caused the same problem so i tired firing the raycast from and empty that was offset halfway up the tile so it was at the end. This worked better but still caused a slight offset when the block changed directions. Even though it was only hundredths of units, over time as the bock keeps getting directions from new tiles it passes the offset becomes more noticeable.

So I ask Unity Answers, is there some way I can get a send message to only happen when the two objects X and Y are identical?

Thanks

I would probably use OnTriggerEnter, with a slightly larger trigger area than the box and as tall as you need for your situation. This is likely to be your most efficient runtime solution.

I believe OnTriggerEnter correctly fires for me if they happen to be inside when the scene starts. If not, you could use the custom solution below once on start up and then fall back to OnTriggerEnter.

Alternatively…

Check if Vector3.Distance() between the two objects is less than some threshold. If it is, then they are “overlapping”. This solves the “coordinates will rarely be exactly the same” problem and it lets you do it with minimal code. If you run this check for many objects on many objects it’s likely to get expensive, especially if you do it every Update(). It also doesn’t scale well, i.e. double the problem set, you more than double the runtime cost. If you just have one box that is moving around, it’s probably ok though.

To ignore a given dimension in the above solution: For each object, just make a new Vector3 for position from the dimensions of the object position, but put the same value, e.g. 0, for the dimension that you don’t care about.

Optimizations:

  • Only do this check under certain conditions, e.g. when the distance check matters and not continuously.
  • Use Vector3.sqrMagnitude() of the difference between the object positions instead of the distance check. It saves a potentially expensive square root calculation. Then, you just use the square of your threshold for “closeness” instead. (That way, you’re comparing like values.)

The raycast approach will also work, but it may be trickier to make these robust or dialed in exactly how you want them.

The script I’ve put down there for you is simple to use and understand. But before you use the script make sure the x,z axes of both the objects are in the centre, when you’re looking from above.

Just another thing to note is that the Y is the vertical plane in Unity. I’m assuming you’re doing this on a plane or a cube or a terrain that is using the default rotation, so if you need to change the “x” to a “y” or the “z” to a “y” in the script below, feel free to do so. This code as required will only execute if the block is EXACTLY above the tile and it does this by checking that the coordinates of the block and tile in 2 of the 3 axes are equal.

    // drag these into the inspector
    var tile: GameObject;
    var block: GameObject;
    
    // the actual code:
    function Update()(
        if ((tile.transform.x == block.transform.x) && (tile.transform.z == block.transform.z)){
            Debug.Log("The block is EXACTLY above the tile.");
            // put your script for changing direction of the block here
        }
    }

Good Luck,

Kourosh S

The other given answer will work 1 out of 1 000 0000 times (if it ever happens…).

Floats are really unlikely to be exactly the same. This is due to inaccuracy. When you have a float that is showing 3.1, in memory it is probable that is looks like 3.099999999999 or 3.10000000000001 and guess what, a computer is that stupid that it considers those to be different.

You need to use approximation or range as due to the reason given above, it won’t work if you want the object to be exactly above.

Nonetheless, a small range will do:

Vector3 pos = tile.position.x;
Vector3 objPos = block.position;
public float range;

if(pos.x < objPos.x - range && pos.x > objPos.x + range && 
pos.z < objPos.z - range && pos.z > objPos.z + range) {
    // The object is within a small square that represents being right on top
}

You can make the range tiny enough so that it won’t show to the user. You can also enlarge the square and lerp the block into the center position of the tile like it is done in some puzzle games.

if(pos.x < objPos.x - range && pos.x > objPos.x + range && 
    pos.z < objPos.z - range && pos.z > objPos.z + range) {
        obj.position = Vector.3.MoveTowards(obj.position, tile.position, 0.2f);
    }