2024-04-20 01:52:41 +02:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class HideWall : MonoBehaviour
|
|
|
|
{
|
|
|
|
public float maxRayDistance;
|
|
|
|
|
|
|
|
[SerializeField] private GameObject target;
|
|
|
|
[SerializeField] private GameObject origin;
|
|
|
|
|
|
|
|
public Material defaultMaterial; // Material when not hitting "Wall"
|
|
|
|
public Material hitMaterial; // Material when hitting "Wall"
|
|
|
|
|
|
|
|
public GameObject prevObject;
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
Vector3 direction = (target.transform.position - origin.transform.position).normalized;
|
|
|
|
|
|
|
|
RaycastHit hit;
|
|
|
|
|
|
|
|
if (Physics.Raycast(origin.transform.position, direction, out hit, maxRayDistance))
|
|
|
|
{
|
|
|
|
if (hit.collider.CompareTag("Wall"))
|
|
|
|
{
|
2024-04-20 15:33:12 +02:00
|
|
|
if (prevObject != null)
|
|
|
|
if (prevObject == hit.collider.gameObject)
|
|
|
|
{
|
|
|
|
var renderer = hit.collider.GetComponent<Renderer>();
|
|
|
|
var originalMat = renderer.material;
|
|
|
|
var originalColor = originalMat.color;
|
|
|
|
originalMat.color = new Color(originalColor.r, originalColor.g, originalColor.b, originalColor.a *0f);
|
|
|
|
renderer.material = originalMat;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var renderer = prevObject.GetComponent<Collider>().GetComponent<Renderer>();
|
|
|
|
var originalMat = renderer.material;
|
|
|
|
var originalColor = originalMat.color;
|
|
|
|
originalMat.color = new Color(originalColor.r, originalColor.g, originalColor.b, originalColor.a + 1f);
|
|
|
|
renderer.material = originalMat;
|
|
|
|
}
|
|
|
|
|
|
|
|
prevObject = hit.collider.gameObject;
|
2024-04-20 01:52:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDrawGizmos()
|
|
|
|
{
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
Gizmos.DrawRay(origin.transform.position, (target.transform.position - origin.transform.position).normalized * maxRayDistance);
|
|
|
|
}
|
|
|
|
}
|