47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
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"))
|
|
{
|
|
if (prevObject == hit.collider.gameObject)
|
|
{
|
|
hit.collider.GetComponent<Renderer>().material = hitMaterial;
|
|
prevObject = hit.collider.gameObject;
|
|
}
|
|
else
|
|
{
|
|
hit.collider.GetComponent<Renderer>().material = defaultMaterial;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawRay(origin.transform.position, (target.transform.position - origin.transform.position).normalized * maxRayDistance);
|
|
}
|
|
}
|