Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alexkaskasoli · Dec 02, 2013 at 09:54 PM · c#rendererenable

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
          }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alexkaskasoli · Dec 02, 2013 at 10:20 PM 0
Share

Thanks! Can't believe I didn't think of that!

avatar image ChinaWhite · Jul 19, 2014 at 10:13 AM 0
Share

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

avatar image robertbu · Jul 19, 2014 at 03:03 PM 0
Share

@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.

avatar image
1

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>();
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

How to change a value in custom shader through script, C# 1 Answer

Re enable the script to recover the initial variables 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges