OldBlueWater/BlueWater/Assets/02.Scripts/CutoutObject.cs

50 lines
1.5 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
public class CutoutObject : MonoBehaviour
{
[SerializeField]
private Transform targetObject;
[SerializeField]
private LayerMask wallMask;
private Camera mainCamera;
private void Awake()
{
mainCamera = GetComponent<Camera>();
}
private void Update()
{
Vector2 cutoutPos = mainCamera.WorldToViewportPoint(targetObject.position);
cutoutPos.y /= (Screen.width / Screen.height);
Vector3 offset = targetObject.position - transform.position;
RaycastHit[] hitObjects = Physics.RaycastAll(transform.position, offset, offset.magnitude, wallMask);
foreach (RaycastHit hit in hitObjects)
{
// Check if the hit object is closer to the camera than the target object
if (hit.distance < offset.magnitude)
{
Material[] materials = hit.transform.GetComponent<Renderer>().materials;
for (int m = 0; m < materials.Length; ++m)
{
materials[m].SetVector("_CutoutPos", cutoutPos);
materials[m].SetFloat("_CutoutSize", 0.25f);
materials[m].SetFloat("_FalloffSize", 0.05f);
}
}
}
}
}
}