Search Unity

Question Hide show mesh renderer of the object when player step in another trigger box

Discussion in 'Editor & General Support' started by projectarcone, Jun 23, 2022.

?

Hide show mesh renderer of the object when player step in another trigger box

  1. yes

    0 vote(s)
    0.0%
  2. no

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. projectarcone

    projectarcone

    Joined:
    Apr 13, 2022
    Posts:
    2
    Hi Guys! I'm starting learning Unity about a week ago and I making my first game. Let's short. My problems is I want to create a TriggerBox (named "HideGround") to trigger to hide and show another object (I already tick Is Trigger box of "HideGround"). I try couple hour to figure it out but I guess I still new and lack of knowledge. Here is my screen shot and code. Hope you guys can help. I appreciate it!

    upload_2022-6-23_16-31-41.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HideGround : MonoBehaviour
    6. {
    7.     public GameObject groundTarget;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.        
    19.     }
    20.  
    21.     private void OnTriggerEnter(Collider other)
    22.     {
    23.         MeshRenderer renderer = groundTarget.GetComponent<MeshRenderer>();
    24.         Debug.Log("Collided!");
    25.  
    26.         if (other.gameObject.CompareTag("Player"))
    27.         {
    28.             Debug.Log("Collide to player!");
    29.             if (renderer.enabled)
    30.             {
    31.                 Debug.Log("Triggered!");
    32.                 renderer.enabled = false;
    33.             }
    34.             else
    35.             {
    36.                 renderer.enabled = true;
    37.             }
    38.         }
    39.     }
    40. }
    41.  
     
  2. projectarcone

    projectarcone

    Joined:
    Apr 13, 2022
    Posts:
    2
    After 1 day, finally I solve the problems. Here is the code and some syntax relate in case someone need. Hope it can help someone.
    With this code you just need 1 Cube and set Trigger to it. When player touch the target object which is box will disable Mesh Renderer and make you can see it but other object still can collide. It's move complicate than just SetActive().

    Other relate syntax: .forceRenderingOff it's also work but little bit buggy.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TriggerBox : MonoBehaviour
    6. {
    7.     [SerializeField] private GameObject Box;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.        
    19.     }
    20.  
    21.     private void OnTriggerEnter(Collider other)
    22.     {
    23.         if (other.gameObject.CompareTag("Player"))
    24.         {
    25.             if (Box.gameObject.GetComponent<MeshRenderer>().isVisible == true)
    26.             {
    27.                 Box.GetComponent<MeshRenderer>().enabled = false;
    28.                 //Box.GetComponent<MeshRenderer>().forceRenderingOff = true;
    29.             }
    30.             else if (Box.gameObject.GetComponent<MeshRenderer>().isVisible == false)
    31.             {
    32.                 Box.GetComponent<MeshRenderer>().enabled = true;
    33.             }
    34.         }
    35.     }
    36. }
    37.  
     
    iarm6089 likes this.