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

63 lines
2.1 KiB
C#
Raw Normal View History

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