Hello. I am a middle school student and am new to unity. I have been creating a 3D game with a third person camera where you explore and collect emeralds. One of the things that I noticed while testing is that the player would be blocked from view by a wall. To fix this I have used a raycast from the camera checking if it’s view is blocked from the player. If it hits an object tagged wall it will get the Mesh Renderer component and disable it. One of the problems I have found is that when it disables one of the walls rendering and the camera moves on to a different wall, it does not re enable the previous wall because I am checking if the raycast is hitting an object tagged as wall and both of the objects hit are tagged as wall. I have attached the code that does this. I really hope this makes sense and thank you to anyone who reads this and responds. Thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraClip : MonoBehaviour
{
public Vector3 rotation;
public GameObject currentObject;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 10f))
{
MeshRenderer renderer = currentObject.GetComponent<MeshRenderer>();
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 10, Color.white);
//I check if the ray has hit a GameObject tagged as wall and if I have I set the current object
//as the object I hit and I disable the renderer.
if (hit.collider.CompareTag("Wall"))
{
currentObject = hit.collider.gameObject;
renderer.enabled = false;
}
//I set the player as the current object and set the renderer to true.
if (hit.collider.CompareTag("Player"))
{
currentObject = hit.collider.gameObject;
renderer.enabled = true;
}
}
}
}