- Home /
Hiding objects between my character and camera, how to show them again when they're no longer in the way?
I'm using the following code to hide objects between the camera and my player:
private void Update() {
Debug.DrawRay(this.transform.position, (this._target.transform.position - this.transform.position), Color.magenta);
RaycastHit[] hits = Physics.RaycastAll(this.transform.position, (this._target.transform.position - this.transform.position), Vector3.Distance(this.transform.position, this._target.transform.position));
foreach (RaycastHit hit in hits) {
Renderer r = hit.collider.renderer;
if (r) {
r.enabled = false;
}
}
}
It works just fine for hiding the objects. But what approach should I take to show these objects again when they're no longer between the player and the camera?
Answer by robertbu · Dec 02, 2013 at 10:01 PM
Sometimes the negative side of things is harder than the positive. One way is to reenable the previously disabled renderers from the previous frame.
Move 'hits' outside of the Update() to the top of the class:
private RaycastHit[] hits = null;
Then at the top of Update():
if (hits != null) {
foreach (RaycastHit hit in hits) {
Renderer r = hit.collider.renderer;
if (r) {
r.enabled = true;
}
}
where did you attach the script to hide the object between the camera and the player? attached to the camera doesnt work for me. cheers
@ChinaWhite - The OPs code above would hide any object found by casting a ray from the camera position to the 'target' position. Not sure what you want to do nor what your code looks like. I suggest you post it as a new question a description of the problem you are trying to solve.
Answer by lelka123 · May 20, 2018 at 09:01 AM
Drop on camera make object you wish to hide with tag of "hideObejct" enjoy
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class HideObjects : MonoBehaviour {
public class HiddenOb
{
public HiddenOb(int TicksToShow, Renderer renderer, Material mat)
{
this.TicksToShow = TicksToShow;
this.renderer = renderer;
oldMat = this.renderer.material;
this.renderer.material = mat;
}
private int TicksToShow;
public Renderer renderer;
private Material oldMat;
public void ResetTicks(int value)
{
TicksToShow = value;
}
public bool AddTick()
{
TicksToShow -= 1;
if(TicksToShow <= 0)
{
renderer.material = oldMat;
return true;
}
return false;
}
}
public Material Fade;
public Material Opaque;
public Transform target;
Camera cam;
Vector3[] ClipPoints;
public void Start()
{
StartCoroutine(ShowObjects());
cam = GetComponent<Camera>();
}
[Range(2, 3)]
public float vl = 2.24f;
public float ds = 0f;
void UpdateClipPoints(Quaternion rot, Vector3 pos)
{
ClipPoints = new Vector3[5];
float z = cam.nearClipPlane;
float x = Mathf.Tan(cam.fieldOfView / vl) * z;
float y = x / cam.aspect;
ClipPoints[0] = (rot * new Vector3(-x, y, z)) + pos;
ClipPoints[1] = (rot * new Vector3(x, y, z)) + pos;
ClipPoints[2] = (rot * new Vector3(-x, -y, z)) + pos;
ClipPoints[3] = (rot * new Vector3(x, -y, z)) + pos;
ClipPoints[4] = pos - transform.forward;
ds = Vector3.Distance(ClipPoints[4], target.position + offset);
}
public Dictionary<Collider, HiddenOb> Hidden = new Dictionary<Collider, HiddenOb>();
public Vector3 offset = Vector3.up;
void Update()
{
UpdateClipPoints(transform.rotation, transform.position);
foreach(Vector3 v3 in ClipPoints)
{
RaycastHit[] hits;
Vector3 p = target.position + offset;
hits = Physics.RaycastAll(p, v3 - p, ds);
foreach (var hit in hits)
{
if (hit.collider.tag == "HideObject")
{
HiddenOb current;
if (Hidden.TryGetValue(hit.collider, out current))
{
current.ResetTicks(10);
}
else
{
current = new HiddenOb(10, hit.collider.GetComponent<Renderer>(), Fade);
Hidden.Add(hit.collider, current);
}
}
}
Debug.DrawRay(p, (v3 - p).normalized * ds, Color.white);
}
Debug.Log(Hidden.Count);
}
List<Collider> Remove = new List<Collider>();
IEnumerator ShowObjects()
{
while(true)
{
yield return new WaitForSeconds(0.10f);
foreach (var item in Hidden.Keys)
{
if (Hidden[item].AddTick())
Remove.Add(item);
}
lock (Hidden)
{
foreach (var item in Remove)
{
Hidden.Remove(item);
}
}
Remove = new List<Collider>();
}
}
}
Your answer
