Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AlexJBoyd · Jun 22, 2013 at 03:43 AM · raycasttriggersendmessagecenter

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KoshX87 · Jun 22, 2013 at 06:20 AM 0
Share

If there are any further problems related to the code Ive put up tell me and I'll help you fix it.

avatar image KoshX87 · Jun 22, 2013 at 06:27 AM 0
Share

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.

avatar image AlexJBoyd · Jun 22, 2013 at 07:22 PM 0
Share

I will give that a try, the issue is however there are multiple tiles so checking every time. If that makes sense?

avatar image KoshX87 · Jun 24, 2013 at 10:02 AM 0
Share

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
 }
 }
 }
avatar image AlexJBoyd · Jun 24, 2013 at 01:25 PM 0
Share

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.

avatar image
0

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);
     }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlexJBoyd · Jun 24, 2013 at 01:23 PM 0
Share

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.

avatar image KoshX87 · Jun 24, 2013 at 09:23 PM 0
Share

my bad =[ but fafase, is the array thing i did right? Or is there still a better way to do it for multiple

avatar image fafase · Jun 25, 2013 at 06:57 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges