From 8c36235d361c90ab8501085d829eadcdf482976a Mon Sep 17 00:00:00 2001 From: kimrdd Date: Sat, 20 Apr 2024 17:29:39 +0200 Subject: [PATCH] Camera pretty much finished --- Assets/Scenes/CameraTestScene.unity | 23 ++++++++-- Assets/Scripts/Camera/HideWall.cs | 67 +++++++++++++++++------------ 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/Assets/Scenes/CameraTestScene.unity b/Assets/Scenes/CameraTestScene.unity index 1537e32..4cdc06e 100644 --- a/Assets/Scenes/CameraTestScene.unity +++ b/Assets/Scenes/CameraTestScene.unity @@ -222,8 +222,8 @@ Transform: m_GameObject: {fileID: 163929259} serializedVersion: 2 m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: 2.8726685, y: 0.40448904, z: -1.4757066} - m_LocalScale: {x: 10, y: 5.455344, z: 0.5} + m_LocalPosition: {x: 2.8726685, y: 0.897, z: -1.4757066} + m_LocalScale: {x: 10, y: 4.470458, z: 0.5} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 1696531492} @@ -550,8 +550,8 @@ Transform: m_GameObject: {fileID: 1425080745} serializedVersion: 2 m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: 2.8726685, y: 0.40448904, z: -11.059507} - m_LocalScale: {x: 10, y: 5.455344, z: 0.5} + m_LocalPosition: {x: 2.8726685, y: 0.9099, z: -11.059507} + m_LocalScale: {x: 10, y: 4.444636, z: 0.5} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 1696531492} @@ -941,6 +941,21 @@ PrefabInstance: propertyPath: m_LocalRotation.z value: -0.1103237 objectReference: {fileID: 0} + - target: {fileID: 3313768392743891090, guid: fd9f9b61c0ebb324ebc9f929e26706bc, + type: 3} + propertyPath: sphereRayOffset.y + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 3313768392743891090, guid: fd9f9b61c0ebb324ebc9f929e26706bc, + type: 3} + propertyPath: maxSphereRayDistanceOffset + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 3313768392743891090, guid: fd9f9b61c0ebb324ebc9f929e26706bc, + type: 3} + propertyPath: maxSphereRayDistanceOffset.y + value: 3 + objectReference: {fileID: 0} - target: {fileID: 5753661378891917460, guid: fd9f9b61c0ebb324ebc9f929e26706bc, type: 3} propertyPath: m_LocalRotation.w diff --git a/Assets/Scripts/Camera/HideWall.cs b/Assets/Scripts/Camera/HideWall.cs index a02ad4b..8e9bc68 100644 --- a/Assets/Scripts/Camera/HideWall.cs +++ b/Assets/Scripts/Camera/HideWall.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using UnityEngine; public class HideWall : MonoBehaviour @@ -11,49 +12,61 @@ public class HideWall : MonoBehaviour public Material hitMaterial; // Material when hitting "Wall" public float sphereRadius; - float maxRayDistance; + float maxSphereRayDistance; + public Vector3 maxSphereRayDistanceOffset; + + private List prevHitObjects = new List(); private void Start() { - maxRayDistance = Vector3.Distance(target.transform.position, origin.transform.position) - sphereRadius * 2; + maxSphereRayDistance = Vector3.Distance(origin.transform.position, target.transform.position) - (sphereRadius * 2 + 1); } void Update() { - Vector3 direction = (target.transform.position - origin.transform.position).normalized; + Vector3 direction = ((target.transform.position + maxSphereRayDistanceOffset) - origin.transform.position).normalized; + RaycastHit[] hits = Physics.SphereCastAll(origin.transform.position, sphereRadius, direction, maxSphereRayDistance); - RaycastHit[] hit; + List hitObjectsThisFrame = new List(); - hit = Physics.SphereCastAll(origin.transform.position, sphereRadius, direction, maxRayDistance); - - for (int i = 0; i < hit.Length; i++) + // Change material of currently hit objects + for (int i = 0; i < hits.Length; i++) { - if (hit[i].collider.CompareTag("Wall")) + if (hits[i].collider.CompareTag("Wall")) { - if (prevObject != null) - if (prevObject == hit[i].collider.gameObject) - { - var renderer = hit[i].collider.GetComponent(); - 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().GetComponent(); - var originalMat = renderer.material; - var originalColor = originalMat.color; - originalMat.color = new Color(originalColor.r, originalColor.g, originalColor.b, 1f); - renderer.material = originalMat; - } + var hitGameObject = hits[i].collider.gameObject; + hitObjectsThisFrame.Add(hitGameObject); - prevObject = hit[i].collider.gameObject; + // Change material only if this object wasn't hit last frame + if (!prevHitObjects.Contains(hitGameObject)) + { + Renderer renderer = hits[i].collider.GetComponent(); + if (renderer != null) + { + renderer.material = hitMaterial; + } + } } - print(hit[i].collider.gameObject.name); } + + // Restore the material of objects that are not hit this frame but were hit in the previous frame + for (int i = 0; i < prevHitObjects.Count; i++) + { + if (!hitObjectsThisFrame.Contains(prevHitObjects[i])) + { + Renderer renderer = prevHitObjects[i].GetComponent(); + if (renderer != null) + { + renderer.material = defaultMaterial; + } + } + } + + // Update the list of previously hit objects + prevHitObjects = hitObjectsThisFrame; } + private void OnDrawGizmos() { Gizmos.color = Color.red;