Camera pretty much finished

This commit is contained in:
kimrdd 2024-04-20 17:29:39 +02:00
parent d6c8946e4e
commit 8c36235d36
2 changed files with 59 additions and 31 deletions

View File

@ -222,8 +222,8 @@ Transform:
m_GameObject: {fileID: 163929259} m_GameObject: {fileID: 163929259}
serializedVersion: 2 serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 2.8726685, y: 0.40448904, z: -1.4757066} m_LocalPosition: {x: 2.8726685, y: 0.897, z: -1.4757066}
m_LocalScale: {x: 10, y: 5.455344, z: 0.5} m_LocalScale: {x: 10, y: 4.470458, z: 0.5}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 1696531492} m_Father: {fileID: 1696531492}
@ -550,8 +550,8 @@ Transform:
m_GameObject: {fileID: 1425080745} m_GameObject: {fileID: 1425080745}
serializedVersion: 2 serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 2.8726685, y: 0.40448904, z: -11.059507} m_LocalPosition: {x: 2.8726685, y: 0.9099, z: -11.059507}
m_LocalScale: {x: 10, y: 5.455344, z: 0.5} m_LocalScale: {x: 10, y: 4.444636, z: 0.5}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 1696531492} m_Father: {fileID: 1696531492}
@ -941,6 +941,21 @@ PrefabInstance:
propertyPath: m_LocalRotation.z propertyPath: m_LocalRotation.z
value: -0.1103237 value: -0.1103237
objectReference: {fileID: 0} 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, - target: {fileID: 5753661378891917460, guid: fd9f9b61c0ebb324ebc9f929e26706bc,
type: 3} type: 3}
propertyPath: m_LocalRotation.w propertyPath: m_LocalRotation.w

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class HideWall : MonoBehaviour public class HideWall : MonoBehaviour
@ -11,49 +12,61 @@ public class HideWall : MonoBehaviour
public Material hitMaterial; // Material when hitting "Wall" public Material hitMaterial; // Material when hitting "Wall"
public float sphereRadius; public float sphereRadius;
float maxRayDistance; float maxSphereRayDistance;
public Vector3 maxSphereRayDistanceOffset;
private List<GameObject> prevHitObjects = new List<GameObject>();
private void Start() 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() 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<GameObject> hitObjectsThisFrame = new List<GameObject>();
hit = Physics.SphereCastAll(origin.transform.position, sphereRadius, direction, maxRayDistance); // Change material of currently hit objects
for (int i = 0; i < hits.Length; i++)
for (int i = 0; i < hit.Length; i++)
{ {
if (hit[i].collider.CompareTag("Wall")) if (hits[i].collider.CompareTag("Wall"))
{ {
if (prevObject != null) var hitGameObject = hits[i].collider.gameObject;
if (prevObject == hit[i].collider.gameObject) hitObjectsThisFrame.Add(hitGameObject);
{
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; // Change material only if this object wasn't hit last frame
if (!prevHitObjects.Contains(hitGameObject))
{
Renderer renderer = hits[i].collider.GetComponent<Renderer>();
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<Renderer>();
if (renderer != null)
{
renderer.material = defaultMaterial;
}
}
}
// Update the list of previously hit objects
prevHitObjects = hitObjectsThisFrame;
} }
private void OnDrawGizmos() private void OnDrawGizmos()
{ {
Gizmos.color = Color.red; Gizmos.color = Color.red;