Use coroutine to change wall transparency

This commit is contained in:
Denis-Cosmin Nutiu 2019-06-22 10:01:55 +03:00
parent 107ef8ef12
commit a8e1613e3f
2 changed files with 22 additions and 11 deletions

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using src.Base; using src.Base;
using src.Helpers; using src.Helpers;
@ -8,6 +9,7 @@ namespace src.Wall
{ {
public class WallTransparency : GameplayComponent public class WallTransparency : GameplayComponent
{ {
public float secondsToWait = 0.1f;
private SpriteRenderer _spriteRenderer; private SpriteRenderer _spriteRenderer;
private Color _originalSpriteColor; private Color _originalSpriteColor;
private bool _isTransparent; private bool _isTransparent;
@ -16,21 +18,27 @@ namespace src.Wall
{ {
_spriteRenderer = GetComponentInChildren<SpriteRenderer>(); _spriteRenderer = GetComponentInChildren<SpriteRenderer>();
_originalSpriteColor = _spriteRenderer.color; _originalSpriteColor = _spriteRenderer.color;
StartCoroutine(nameof(CheckIfSomethingNear));
} }
private void Update() private IEnumerator CheckIfSomethingNear()
{ {
var position = transform.position; while (true)
/* RayCast from the center of the tile up one distance and set layerMask to Player only! */
var hit = Physics2D.Raycast(new Vector2(position.x + .5f, position.y + 0.5f),
Vector2.up, 1f, 1 << 15);
if (hit.collider)
{ {
BecomeTransparent(); var position = transform.position;
} /* RayCast from the center of the tile up one distance and set layerMask to Player only! */
else var hit = Physics2D.Raycast(new Vector2(position.x + .5f, position.y + 0.5f),
{ Vector2.up, 1f, 1 << 15);
BecomeOpaque(); if (hit.collider)
{
BecomeTransparent();
}
else
{
BecomeOpaque();
}
yield return new WaitForSeconds(secondsToWait);
} }
} }

View file

@ -32,6 +32,9 @@ GraphicsSettings:
- {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0} - {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 10770, guid: 0000000000000000f000000000000000, type: 0} - {fileID: 10770, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 10783, guid: 0000000000000000f000000000000000, type: 0} - {fileID: 10783, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0}
m_PreloadedShaders: [] m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000, m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0} type: 0}