Search Unity

Collision Triggered, Tile Opacity changes.

Discussion in '2D' started by Namix1, May 28, 2019.

  1. Namix1

    Namix1

    Joined:
    May 28, 2019
    Posts:
    1
    I'm building an Isometric 2D game and i want to have a trigger that lower the opacity of a tile should you be behind it. I'm attempting to use Collision triggers that are in the areas that are behind the tile. I've put the Composite Collider as a child of the tile map that I want to decrease in opacity. My script will not work at all this is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Tilemaps;


    public class TilemapOpacity : MonoBehaviour
    {


    public float NO = .5f;





    // Start is called before the first frame update
    void Start()
    {


    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.gameObject.tag == "Trig1")
    {
    NO -= .5f;
    GetComponentInParent<Tilemap>().color = new Color(1, 1, 1, NO);

    }


    }

    void OnCollisionExit2D(Collision2D collision)
    {
    if (collision.gameObject.tag == "Trig1")
    {
    NO += .5f;
    GetComponentInParent<Tilemap>().color = new Color(1, 1, 1, NO);

    }
    }



    }
    [/code]

    please help!!!!!!!!!!!!! thanks
     
  2. EmmaEwert

    EmmaEwert

    Joined:
    Mar 13, 2014
    Posts:
    30
    Did you try checking whether the collisions ever happen? What do you mean by "My script will not work at all"?

    An alternative solution that has worked for me before is to add a second, identical sprite to your character, set its opacity to 0.5f, and put its sorting order (order in layer) to like 100 so it always shows the half-transparent character sprite through everything, and you can't see it if the fully opaque sprite is visible too (because opacity 1 + opacity 0.5 is still opacity 1)
     
  3. Namix0

    Namix0

    Joined:
    Apr 16, 2019
    Posts:
    1
    I apologize but i'm a complete novice at C# and Unity how would one check to see if the collision happens?
     
  4. EmmaEwert

    EmmaEwert

    Joined:
    Mar 13, 2014
    Posts:
    30
    That's okay!

    The easiest way is probably just to call Debug.Log("Yay we're colliding"); inside the OnCollisionEnter2D. Colliders are kind of finnicky though (have to have a correctly set up rigidbody on at least one of the colliders' gameobjects, needs to be dynamic or have full collisions enabled, there's tonnes of gotchas I'm afraid :(