- Home /
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
Answer by s_guy · Jun 25, 2013 at 07:08 PM
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.
Answer by KoshX87 · Jun 22, 2013 at 06:18 AM
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
If there are any further problems related to the code Ive put up tell me and I'll help you fix it.
and if this code helps or works please click on the tick next to this answer. This is because in the future if someone needs help with a similar question, it will easier for them to find the solution and save the time of someone else having to wewrite all of this again.
I will give that a try, the issue is however there are multiple tiles so checking every time. If that makes sense?
just give all the tiles a tag, such as "tile" and make an array of tiles: var tileArray: GameObject[];
ins$$anonymous$$d of using the inspector. make a Start() function and type in the following: function Start(){ tileArray = GameObject.FindGameObjectsWIthTags("tile"); }
now on the start of the script the tileArray will contain all the gameobjects in the game that contain the tag "tile". From here you just need to use the following code ins$$anonymous$$d of what was in the first script i wrote:
function Update(){
for (n=0;n<tileArray.length;n++){
if ((tileArray[n].transform.x == block.transform.x)&&(tileArray[n].transform.z == block.transform.z){
//change block direction
}
}
}
As mentioned in the comment above the odds of the X/Y lining up perfectly for a moving object are rather slim, and I think then having the forloop check it makes it even more unlikely. I will explore more option tonight, thank you though for answering and following up.
Answer by fafase · Jun 24, 2013 at 10:28 AM
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);
}
I think this may be the better way to handle the situation, I will be giving it a try later tonight. If this works I will be sure to tick it as correct.
my bad =[ but fafase, is the array thing i did right? Or is there still a better way to do it for multiple
Since there are many tiles, yes the array principle does work and is probably the best process. Another way would be to cast a ray below and only compare with the hitting tile. If you only have about ten of them, just go for array, if you have hundreds or more then maybe the raycast will make a little different. Nonetheless, this is dealing with array and float comparison which are both extremely fast.
Your answer
Follow this Question
Related Questions
Raycast is being unreliable 1 Answer
DontGoThroughThings vs Triggers 1 Answer
Raycast not updating inside a trigger collider 0 Answers
Performance issues with SendMessage in FixedUpdate using Raycast 0 Answers
How do I detect if there's an object at the same position and destroy ONLY ONE of them? 1 Answer