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 PprTiger · Jan 31, 2020 at 05:58 AM · colliderunity 2dtilemaptransparency

How to make 2D walls Transparent when the player or enemy is behind it.

I've hit a dead end and tried everything I could think of.

I'm making a 2d Isometric game so I need to make specific walls transparent since I can't rotate the camera. My initial solution was to add an empty GameObject with a Polygon Collider as a child of the wall. When the player/enemy enters the "zone" of the Polygon Collider, the specific wall it's attached to will go invisible. Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class TriggerChangeOpacityTiles : MonoBehaviour
 {
     Tilemap m_Renderer;
 
     // Start is called before the first frame update
     void Start()
     {
         //Grabs the Tilemap Component
         m_Renderer = GetComponentInParent<Tilemap>();
     }
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         Debug.Log("Player has entered the trigger");
 
         //Turns object invisible when the player enters its collider space
         if (other.gameObject.tag == "Player")
         {
             m_Renderer.color = new Color(1f, 1f, 1f, 0.4f);
         }
     }
 
     private void OnTriggerExit2D(Collider2D other)
     {
         Debug.Log("Player has exited the trigger");
 
         //Turns object visible when player enters its collider space
         if (other.gameObject.tag == "Player")
         {
             m_Renderer.color = new Color(1f, 1f, 1f, 1f);
         }
     }
 }



And it worked... Too well.

alt text



Should've seen it coming when I used the Tilemap's Alpha. I know there's a code that allows me to affect a specific tile at the exact coordinates, but I don't wanna do that with every tile. Is there another solution?

untitled.jpg (420.1 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LeFlop2001 · Jan 31, 2020 at 09:48 AM

 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class Test : MonoBehaviour
 {
     public Tilemap tm;
     Vector3Int tileToFade;
     private void Update()
     {   
         tm.SetTileFlags(tileToFade, TileFlags.None);
         tm.SetColor(tileToFade, Color.Lerp(tm.GetColor(tileToFade),Color.clear,0.1f));
     }
 }

Instead of using boxcolliders, you could use a script that checks the players position (Vector3(x,y,0)) and the lower tiles (Vector3(x-1,y-1,0)) and applies the code above when those tiles are walls

 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class Test : MonoBehaviour
 {
     public Tilemap tm;
     Vector3Int playerPosition;
     Tile wall;
     private void Update()
     {
         int x = playerPosition.x;
         int y = playerPosition.y;
         if (tm.GetTile(new Vector3Int(x - 1, y - 1, 0)) == wall)
         {
             tm.SetTileFlags(new Vector3Int(x-1,y-1,0), TileFlags.None);
             tm.SetColor(new Vector3Int(x - 1, y - 1, 0), Color.Lerp(tm.GetColor(new Vector3Int(x - 1, y - 1, 0)), Color.clear, 0.1f));
         }
         if (tm.GetTile(new Vector3Int(x + 1, y - 1, 0)) == wall)
         {
             tm.SetTileFlags(new Vector3Int(x + 1, y - 1, 0), TileFlags.None);
             tm.SetColor(new Vector3Int(x + 1, y - 1, 0), Color.Lerp(tm.GetColor(new Vector3Int(x + 1, y - 1, 0)), Color.clear, 0.1f));
         }
 
     }
 }

Comment
Add comment · Show 2 · 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 PprTiger · Feb 01, 2020 at 02:59 AM 0
Share

I've tried it, but it doesn't seem to work. The player walks on the wall's adjacent ground tiles but the wall stays opaque.

Do I attach the script to the Wall Tiles, the Ground Tiles, or the Player? Does it check the tile from the Sorting layer or the GameObject tag?

avatar image LeFlop2001 PprTiger · Feb 01, 2020 at 08:29 PM 0
Share

Sadly the code I posted is somewhat a pseudo code and won't work without properly adjusting it. I would write you the actual code but sadly, since you're using an isometric tile map, I couldn't figure out how to convert a world position to the tilemap position. The tile positions saved in the tile map are at an 45 degree offset( if you alter the tile (1,0,0) the tile is at (1,1,0) in world Space). If you were to use a normal tile map this would work(simply attach it to the wall tilemap and drag the player into the playerfield in the inspector)

 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class Test : $$anonymous$$onoBehaviour
 {
     public Transform player;
     public Tilemap tm;
 
     public void Update()
     {
         tm.RefreshAllTiles();
 
         int x = $$anonymous$$athf.RoundToInt(player.position.x - 0.5f);
         int y = $$anonymous$$athf.RoundToInt(player.position.y - 0.5f);
         Vector3Int pp = new Vector3Int(x,y-1,0);
 
         tm.SetTileFlags(pp, TileFlags.None);
         Debug.Log(tm.GetColor(new Vector3Int(pp.x, pp.y + 1, 0)));
         tm.SetColor(pp, Color.clear);
     }
 }

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

164 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

Related Questions

My character is floating. How do I fix this? 1 Answer

Transparrent tiles not working with tilemap collider 2 Answers

Touch Detection in 2D Game 3 Answers

Tilemap Glitching 0 Answers

Tilemap for mobile 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