Search Unity

Question Need help thinking of a solution to select/hover a 3D element in another

Discussion in 'C# Job System' started by Lyawii, Dec 9, 2022.

  1. Lyawii

    Lyawii

    Joined:
    Dec 4, 2018
    Posts:
    8
    Hello !

    I'm doing a project and I have a task that I can't find an idea to solve.

    The task is to be able to select or highlight a 3D element contained in another.

    I explain, my model is a skeleton containing all its organs, the skeleton can rotate on its 3 axes and move in space and all its organs follow it.
    I would like to be able to change the colour of an organ (to yellow for example) when the mouse passes over it or selects it, obviously when the skeleton is still.

    The biggest problem I have is getting Unity to detect my mouse on child objects of the skeleton.
    I also point out that the skeleton has a collider to interact with.

    I hope I'm clear enough, if not I'll try to clarify my request.
    I thank you for sharing your ideas to help me accomplish my task!

    Thank you very much,
     
  2. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    398
    Not sure this is regarding the job system really, but I asked AI and it said:


    To highlight a 3D element contained inside another using Unity C#, you can follow these steps:

    1. Create a new C# script and attach it to the 3D element that you want to highlight (e.g. the organ).

    2. In the script, add a public variable of type Material to hold the highlight material. You can use this material to change the color of the 3D element when it is highlighted.

    3. In the Start() method of the script, set the default material of the 3D element to the highlight material. This will ensure that the element is highlighted by default when the scene is first loaded.

    4. Add a private variable of type Renderer to hold a reference to the renderer component of the 3D element. This will allow you to access and modify the material of the 3D element.

    5. In the Start() method, get a reference to the renderer component of the 3D element and store it in the Renderer variable that you created.

    6. Add an OnMouseOver() method to the script, which will be called whenever the mouse cursor moves over the 3D element. In this method, you can change the material of the 3D element to a different material (e.g. a yellow material) to highlight it.

    7. Add an OnMouseExit() method to the script, which will be called whenever the mouse cursor moves away from the 3D element. In this method, you can change the material of the 3D element back to the default highlight material to un-highlight it.
    Here is an example script that shows how to highlight a 3D element using these steps:



    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class HighlightElement : MonoBehaviour
    4. {
    5.     // The material to use for highlighting
    6.     public Material highlightMaterial;
    7.  
    8.     // The default material of the 3D element
    9.     private Material defaultMaterial;
    10.  
    11.     // A reference to the renderer component of the 3D element
    12.     private Renderer renderer;
    13.  
    14.     void Start()
    15.     {
    16.         // Set the default material to the highlight material
    17.         defaultMaterial = highlightMaterial;
    18.  
    19.         // Get a reference to the renderer component of the 3D element
    20.         renderer = GetComponent<Renderer>();
    21.     }
    22.  
    23.     void OnMouseOver()
    24.     {
    25.         // Change the material of the 3D element to the yellow material
    26.         renderer.material = yellowMaterial;
    27.     }
    28.  
    29.     void OnMouseExit()
    30.     {
    31.         // Change the material of the 3D element back to the default material
    32.         renderer.material = defaultMaterial;
    33.     }
    34. }
    35.  
    You can then attach this script to the 3D element that you want to highlight, and set the 'highlightMaterial' variable to the material that you want to use for highlighting. When the mouse cursor moves over the 3D element, it will be highlighted with the yellow material, and when the mouse cursor moves away, it will return to its default highlighted state.