Menu Close

Coding: Gizmos

Gizmo above the house. The inhabitants are only 10% satisfied.

Gizmos are small icons in the game field, which float above an object and tell the player something. They are very easy to create and can be expanded as desired. The following tutorial will only show the basics on which you can build better Gizmo systems in Unity3D.

Instructions

  1. Create a sprite (no image!) in the object hierarchy and add a text element. I used TextmeshPro:

2. Now you need a small script. The script will be explained very briefly later:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class demoscript : MonoBehaviour
{
    public GameObject gizmo_wood_number;
    public GameObject my_house;
    public Camera MainCamera;

    private bool gizmos_are_visible = false;

    void LateUpdate()
    {
        if (gizmos_are_visible == true)
        {
            gizmo_wood_number.transform.position = new Vector3(my_house.transform.position.x, my_house.transform.position.y + 1, my_house.transform.position.z);
            gizmo_wood_number.transform.LookAt(MainCamera.transform.position, Vector3.up);
        }
    }

    void Reset_Gizmo()
    {
        gizmos_are_visible = false;
        gizmo_wood_number.SetActive(false);
    }
}

The script takes care in the LateUpdate() routine that the gizmo always looks towards the camera. Then it makes sure that the gizmo always stays at the house – in my example my_house. But the gizmo is displayed +1 higher than the house, so that the gizmo is not behind the house or inside the house. You might have to adjust this value.

Then you have to call the function Reset_Gizmo() yourself to hide the gizmo again.

To make the gizmo appear, just set the value gizmos_are_visible to TRUE somewhere in the script and the gizmo is visible again.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Put 'Pax Augusta' on your STEAM WISHLIST now!

X