Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 fortnitesucks66 · Nov 16, 2018 at 04:18 PM · collisioncollider2dtilemaptiles

How to detect on which exact tile a collision happened?

I have a tilemap with a TIlemapCollider2D with it's trigger on true, and a script that detects the collision with the tilemap through OnTriggerEnter2D. It detects collision, but I wan't to know the exact position of the Tile that detected the collision. Is there any way to do that?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Tomer-Barkan · Nov 16, 2018 at 06:00 PM

For starters, you need the collision point. OnTriggerEnter2D is very problematic on this aspect, since it doesn't provide collision data, only the collider. So you don't have the contact points. You can either calculate them manually based on the position of the hitting object, or manually do a physics raycast on the collider to get the collision info. This is a bit of an oversight on Unity's side. Another option is to use a non-trigger collider and make the rigidbody kinematic, so it will not be affected by forces. See here. If you use OnCollisionEnter2D then you can use collision.GetContacts() to find the contact points.


Now, once you have the contact points, you can use the following:

  • use GetComponent() on the collider to get the tilemap itself.

  • use Tilemap.layoutGrid to get the grid from the TileMap.

  • use Grid.WorldToCell with the collision point to get the cell coordinates in which the collision point is.

  • use TileMap.GetTile or TileMap.GetInstantiatedObject to get the TileBase or GameObject in the collision tile.

Comment
Add comment · Show 4 · 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 fortnitesucks66 · Nov 16, 2018 at 07:17 PM 0
Share

Even if i change the function to OnCollisionEnter2D it can only give me the points of collision, but not the exact position of the tile that i collided with.

avatar image Tomer-Barkan fortnitesucks66 · Nov 16, 2018 at 07:38 PM 0
Share

I gave you a long list of commands on how to get the tile from the collision points... Did they not work? What was the problem?

avatar image fortnitesucks66 Tomer-Barkan · Nov 16, 2018 at 10:30 PM 0
Share

I didn't try the raycast method, but I highly supect it would give me the same results as if I use collision.GetContacts(). The problem was that the position where the object collided with the tilemap isn't the same as the exact position of the tile, even if I use WorldToCell().

Show more comments
avatar image
0

Answer by Shlomito · Jun 02 at 03:00 AM

@Tomer-Barkan wouldn't the tiles be solid if IsTrigger is off, even if it has a kinematic rigidbody? When I try it it is, and the whole point of IsTrigger being on in the first place is so that other objects can go through it.

It may be related to newer Unity versions replacing the isKinematic box with the Dynamic/Kinematic/Static options, but idk how to do it otherwise. I was using OnTriggerEnter and using the ClosestPoint function with the player's position but it didn't seem to be reliable.

Edit: I found that as long as the collider of the tile is not touching the edges, and the player is less than a tile big, this approach does work, but it's certainly not perfect.

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

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

154 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

The tilemap collider is simply not working 0 Answers

Tilemap Collider 2D not being created at runtime 0 Answers

Getting the Collider of a Tile 0 Answers

Composite Collider on Tilemap not really stopping player 0 Answers

Destroy Tiles That Collide With Object 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