3DTD/Assets/Scripts/Camera/HideWall.cs

63 lines
2.1 KiB
C#

using UnityEngine;
public class HideWall : MonoBehaviour
{
[SerializeField] private GameObject target;
[SerializeField] private GameObject origin;
public GameObject prevObject;
public Material defaultMaterial; // Material when not hitting "Wall"
public Material hitMaterial; // Material when hitting "Wall"
public float sphereRadius;
float maxRayDistance;
private void Start()
{
maxRayDistance = Vector3.Distance(target.transform.position, origin.transform.position) - sphereRadius * 2;
}
void Update()
{
Vector3 direction = (target.transform.position - origin.transform.position).normalized;
RaycastHit[] hit;
hit = Physics.SphereCastAll(origin.transform.position, sphereRadius, direction, maxRayDistance);
for (int i = 0; i < hit.Length; i++)
{
if (hit[i].collider.CompareTag("Wall"))
{
if (prevObject != null)
if (prevObject == hit[i].collider.gameObject)
{
var renderer = hit[i].collider.GetComponent<Renderer>();
var originalMat = renderer.material;
var originalColor = originalMat.color;
originalMat.color = new Color(originalColor.r, originalColor.g, originalColor.b, 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, 1f);
renderer.material = originalMat;
}
prevObject = hit[i].collider.gameObject;
}
print(hit[i].collider.gameObject.name);
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(origin.transform.position, sphereRadius);
}
}