You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
18 Hrs
50 Min
16 Seg
Curso de Herramientas de Unity

Curso de Herramientas de Unity

Hederson Carrasquero

Hederson Carrasquero

Gizmos

7/20
Resources

What are gizmos and handles in Unity?

Gizmos and handles are fundamental tools in Unity that allow for effective visual debugging. Through a specific library, developers can visualize a variety of shapes in the scene, such as lines, spheres and more. This capability is useful not only for debugging, but also for developing custom tools. Let's explore in detail how they work and how to implement them in your Unity projects.

How to use a package in Unity to visualize gizmos?

To get started with gizmos in Unity, an initial step is to import a special package that you can find in the resources section. This package must be opened with Unity, and, by double-clicking it, you will be able to import all its contents. Once imported, you will have access to special scenes, such as one called "gizmos", which illustrates this functionality through a visual representation of a lumberjack and a ghost in an environment.

Initial setup

  1. Create a new layer:
    • Head to the layers section and create a new layer called "player".
  2. Assign the layer to the characters:
    • Make sure that the ghost and the lumberjack have the "player" layer set correctly.

Checking before running

At the start of the scene, the ghost will attempt to chase the lumberjack when it gets close enough. However, at first, there is no visual indication of this on the game screen. This is where gizmos become essential for visualizing the character's trajectories and states.

How do I set up gizmos for visualization in the editor?

Manipulating gizmos in Unity is simple and useful to identify the inner workings of the elements in your scene.

Example code for adding gizmos

private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, visionRadius);}

This code snippet allows a red sphere to be drawn around the ghost, indicating its range of vision. You can change the colors and transparency to improve the display.

State-based color change

It is possible to improve the visual feedback by checking the state and altering the color of the gizmo. For example, we can make the ghost sphere change to green when the lumberjack is in range:

Gizmos.color = isInRange ? Color.green : Color.red;

This translates into more clarity and precision when working and debugging the game.

What other visual tools can be integrated?

Drawing raycasts

In addition to spheres, visual raycasting can be added to better understand the behaviors of the ghost and its environment. This can be accomplished by using Debug.DrawRay within the FixedUpdate method:

Debug.DrawRay(transform.position, direction * checkDistance, Color.red);

Thus, a red ray will indicate where it is looking and how it interacts with the world, helping to evaluate collisions and character decisions.

Exploration and continuous improvement

Experiment with different shapes and colors of gizmos and explore Unity's documentation to discover more ways to represent information visually in the editor. Creative implementation of these tools can revolutionize the way you understand and refine your game projects.

This knowledge and practice prepares you to develop even more advanced visual tools, such as handles, which we will cover in future classes. Keep exploring and developing your Unity skills to create awesome game experiences!

Contributions 2

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Aqu铆 hay un peque帽o articulo sobre como usar algunos **gizmos.** Enlace: <https://temas-gabriel.blogspot.com/2023/07/gizmos-en-unity.html>
condicionales con operador ternario <https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator>