Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Vaulcul · Jan 10, 2019 at 04:03 PM · scripting problemgameobject2d gametilemap

Find Tilemap Player is Currently At

Hi there!


EDIT: The main question I have is "How would you get the name of the "Tilemap" (not tile) located at the player object location?" Below is a really long way of asking that.


I'm trying to create something similar to the Zelda II: Adventure of Link overworld, where the player traverses on a map and enemies spawn randomly. If the player collides w/ an enemy they are taken to a scene that corrisponds with the type of landscape they were on before the collision. So, if they were on a grassy spot, the scene would be grassy, if they were on a wooded spot, the scene would be wooded... etc.

For a visual idea of how this would work, see the following video from 1:25 to 1:55 (the commentator also talks about how this functions here) www.youtube.com/watch?v=6k2bn6vech0&list=PLC8EABAF343E1BD4E

I know that there are several elements that would make this work, and I think I can piece together most of it (I'm subpar at programming currently).

I'm using the Unity Tilemap System in Unity 2018.3

The part I'm having trouble with right now, is getting the tile type (Plains, Swamp, Forest, etc.) from the tiling system.

Here's what I've tried thus far (maybe one of these is partially my answer... I just don't know the next step):

Attempt #1 -

I found the following code snippet in the Unity Documentation:

https://docs.unity3d.com/ScriptReference/GameObject-tag.html

 using UnityEngine;
 
 public class CollisionGameObjectExample : MonoBehaviour
 {
     //Detect collisions between the GameObjects with Colliders attached
     void OnCollisionEnter(Collision collision)
     {
         //Check for a match with the specified name on any GameObject that collides with your GameObject
         if (collision.gameObject.name == "MyGameObjectName")
         {
             //If the GameObject's name matches the one you suggest, output this message in the console
             Debug.Log("Do something here");
         }
 
         //Check for a match with the specific tag on any GameObject that collides with your GameObject
         if (collision.gameObject.tag == "MyGameObjectTag")
         {
             //If the GameObject has the same tag as specified, output this message in the console
             Debug.Log("Do something else here");
         }
     }
 }


I know it's not on Tilemaps, but I thought I could maybe use the a Tilemap Collider 2D as the collider for the GameObject and attach the code to my player object, as most of my map terrains/environments are housed in their own Tilemap/GameObject (under the Grid), and then making Tilemap Colliders triggers. But, when I did this the character ceased to be able to pass through the terrain/environment and there were no Debug Logs firing on collision.

Attempt #2 -

I use the following code I've piecemealed together from documentation and forum posts:

 private GameObject

 playerObj = null;
 Vector3 playerLocV;
 float playerX = 0;
 float playerY = 0;
 float playerZ = 0;
     
 
     void Start()
     {
         if (playerObj == null)
             playerObj = GameObject.Find("OverworldCharacter");
     }
     
     void Update()        
     {
 
         
         playerX = playerObj.transform.position.x;
         playerY = playerObj.transform.position.y;
         playerZ = playerObj.transform.position.z;
         playerLocV = new Vector3(playerX, playerY, playerZ);
         
         Vector3Int local = Vector3Int.FloorToInt(transform.InverseTransformPoint(playerLocV));
         Tilemap map = GetComponent<Tilemap>();
         TileBase tile = map.GetTile(local);
         //Debug.Log(tile);
                Debug.Log(map);  //This returns the desired data type, but the information is not useful.
     }
     
 }

While this code does "work", it only works when I attach it to a (every) Tilemap object... and the information it outputs is not accurate or useful. As it spits out the name of every Tilemap object that has the script associated with it, regardless of whether the player object is near the Tilemap object or not.

EDIT: I've found that the information that is provided from 'map' is pretty much what I'm looking for as far as data type... it's still not accurate or useful, and I'm not sure how to manipulate the line of code for what I'm trying to do.

Ideally, I would have some sort of listener on the player object to see what TileMap/GameObject is directly beneath them... I'm just not sure how to get there from here.

Thanks for your help in advance.

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

0 Replies

· Add your reply
  • Sort: 

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

171 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 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

How to make an object invisible before it is triggered to show? 2 Answers

How can I achieve movement on hextile? 0 Answers

How do I access the exact tile my player is colliding with?,How can i get access to the specific tile my player is colliding with? 0 Answers

Why are these object passing through each other? 1 Answer

Calculating Scrolling GameObject x position scrolling pass another GameObject x postion (2D Game) 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