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

73 lines
2.3 KiB
C#
Raw Normal View History

2024-04-20 17:29:39 +02:00
using System.Collections.Generic;
2024-04-20 01:52:41 +02:00
using UnityEngine;
public class HideWall : MonoBehaviour
{
[SerializeField] private GameObject target;
[SerializeField] private GameObject origin;
2024-04-21 03:22:56 +02:00
[SerializeField] private TowerCam towerCamScript;
2024-04-20 20:55:22 +02:00
[Range(1f, 10f)] public float hideFadeSpeed;
[Range(1f, 10f)] public float showFadeSpeed;
2024-04-20 01:52:41 +02:00
2024-04-20 16:52:39 +02:00
public float sphereRadius;
2024-04-21 00:40:14 +02:00
[Range(0, 10)] public float sphereRange;
public Vector3 sphereOffset;
2024-04-20 17:29:39 +02:00
2024-04-20 20:55:22 +02:00
float maxSphereRayDistance;
2024-04-20 17:29:39 +02:00
private List<GameObject> prevHitObjects = new List<GameObject>();
2024-04-20 16:52:39 +02:00
2024-04-20 01:52:41 +02:00
void Update()
{
2024-04-21 03:22:56 +02:00
if (target == null && towerCamScript != null)
{
target = towerCamScript.m_gameObject;
}
2024-04-21 00:40:14 +02:00
maxSphereRayDistance = Vector3.Distance(origin.transform.position, target.transform.position) - (sphereRadius * 2) + sphereRange;
Vector3 direction = ((target.transform.position + sphereOffset) - origin.transform.position).normalized;
2024-04-20 17:29:39 +02:00
RaycastHit[] hits = Physics.SphereCastAll(origin.transform.position, sphereRadius, direction, maxSphereRayDistance);
2024-04-20 16:52:39 +02:00
2024-04-20 17:29:39 +02:00
List<GameObject> hitObjectsThisFrame = new List<GameObject>();
2024-04-20 01:52:41 +02:00
2024-04-20 23:18:34 +02:00
2024-04-20 17:29:39 +02:00
for (int i = 0; i < hits.Length; i++)
2024-04-20 01:52:41 +02:00
{
2024-04-21 00:40:14 +02:00
//print(hits[i].collider.gameObject.name);
2024-04-20 23:18:34 +02:00
if (hits[i].collider != null)
2024-04-20 01:52:41 +02:00
{
2024-04-20 17:29:39 +02:00
var hitGameObject = hits[i].collider.gameObject;
hitObjectsThisFrame.Add(hitGameObject);
2024-04-20 20:55:22 +02:00
var wallOpacity = hitGameObject.GetComponent<WallOpacity>();
if (wallOpacity != null && !prevHitObjects.Contains(hitGameObject))
2024-04-20 17:29:39 +02:00
{
2024-04-20 20:55:22 +02:00
wallOpacity.HideWall(hideFadeSpeed);
2024-04-20 17:29:39 +02:00
}
}
}
2024-04-20 15:33:12 +02:00
2024-04-20 20:55:22 +02:00
foreach (var prevHitObject in prevHitObjects)
2024-04-20 17:29:39 +02:00
{
2024-04-20 20:55:22 +02:00
if (!hitObjectsThisFrame.Contains(prevHitObject))
2024-04-20 17:29:39 +02:00
{
2024-04-20 20:55:22 +02:00
var wallOpacity = prevHitObject.GetComponent<WallOpacity>();
if (wallOpacity != null)
2024-04-20 17:29:39 +02:00
{
2024-04-20 20:55:22 +02:00
wallOpacity.ShowWall(showFadeSpeed);
2024-04-20 17:29:39 +02:00
}
2024-04-20 01:52:41 +02:00
}
}
2024-04-20 17:29:39 +02:00
prevHitObjects = hitObjectsThisFrame;
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
}
}