Debug Draw Circle Cast Unity 2d

Testing and debugging are important to make sure your game doesn't behave in a style that it isn't supposed to. Especially, when it comes to judging the distance of something or when you're edifice a scene, it is helpful to have some sort of visual aid to help make a more accurate spacing.

The Gizmos course is a collection of methods that tin can be used to describe lines or shapes onto the Scene view in Unity Editor. The visuals drawn via these methods will not be visible in the game window in the editor and the built version of the game.

These methods are normally used for confirming the distance of Raycasts and aiding with setting up your game'southward scenes.

Table of Contents

  • How To Utilise Gizmos
  • How To Draw A Debug Line With Gizmos in Unity
  • How To Draw A Debug Ray With Gizmos in Unity
  • How To Draw A Debug Sphere With Gizmos In Unity
  • How To Draw A Debug Cube With Gizmos In Unity
  • Conclusion

How To Use Gizmos

Get-go of all, all Gizmos methods have to be called from inside either the OnDrawGizmos issue method or the OnDrawGizmosSelected event method of a script.

This is because these 2 upshot methods are called whenever there's an update within the Scene view in Unity Editor.

So, what are the difference between these 2 events?

OnDrawGizmos

The OnDrawGizmos consequence displays the Gizmos drawings on the Scene view at all times.

A Ray drawn in OnDrawGizmos event.
            

using System.Collections; using Organisation.Collections.Generic; using UnityEngine; public grade VisualAid : MonoBehaviour { private void OnDrawGizmos() { Gizmos.colour = Color.dark-green; Vector2 direction = new Vector2(one.5f, 0); Gizmos.DrawRay(transform.position, direction); } }

Code linguistic communication: C# ( cs )

OnDrawGizmosSelected

On the other manus, the OnDrawGizmosSelected upshot displays the Gizmos drawings only when the game object that the script is attached to is selected.

A Ray drawn in OnDrawGizmosSelected event.
            

using Organization.Collections; using System.Collections.Generic; using UnityEngine; public class VisualAid : MonoBehaviour { private void OnDrawGizmosSelected() { Gizmos.colour = Color.green; Vector2 management = new Vector2(1.5f, 0); Gizmos.DrawRay(transform.position, direction); } }

Lawmaking language: C# ( cs )

Irresolute The Color Of Gizmos

The color of the lines being fatigued past the Gizmos methods tin can be changed by setting the color holding of the Gizmos class before drawing.

Any Gizmos drawn after setting the color will take the same color until a new color is set.

            

using Organisation.Collections; using System.Collections.Generic; using UnityEngine; public class VisualAid : MonoBehaviour { private void OnDrawGizmos() { // Set the color of Gizmos to green Gizmos.color = Color.green; Vector2 direction = new Vector2(ane.5f, 0); Vector2 direction2 = new Vector2(1.5f, 0.5f); // These ii rays will be drawn in light-green Gizmos.DrawRay(transform.position, management); Gizmos.DrawRay(transform.position, direction2); // Then we prepare the color of Gizmos to crimson Gizmos.colour = Color.red; Vector2 direction3 = new Vector2(1.5f, -0.5f); // This ray below volition be scarlet Gizmos.DrawRay(transform.position, direction3); } }

Lawmaking language: C# ( cs )

The code above draws 2 rays in dark-green color and one ray in red color.

Multiple Gizmos rays with different colors

Note that the lines and shapes drawn with the Gizmos methods are non visible in the game view and the built version of the game. Gizmos are meant for debugging only.

Debug lines are not visible in the game view

How To Draw A Debug Line With Gizmos in Unity

            

Gizmos.DrawLine(fromPosition, toPosition);

Code linguistic communication: C# ( cs )

The Gizmos.DrawLine method is used to draw a debug line onto the Scene view starting from a specified position to a specified position.

            

using System.Collections; using Organization.Collections.Generic; using UnityEngine; public class VisualAid : MonoBehaviour { private void OnDrawGizmos() { // Set the color of Gizmos to light-green Gizmos.colour = Color.greenish; Vector2 from = new Vector2(-1f, -0.5f); Vector2 to = new Vector2(1f, 0.5f); // Draw a line from (-ane, -0.five) to (1, 0.5) Gizmos.DrawLine(from, to); } }

Code language: C# ( cs )

The example higher up draws a green line from the position (-1, -0.5) to the position (1, 0.5).

Line drawn with the Gizmos.DrawLine method.

Employ Vector3 instead if you want to depict a line in a 3D space.

Note that these positions are non relative to the game object's position. Adjust the positions appropriately if you want them to exist based on the object's position.

How To Describe A Debug Ray With Gizmos in Unity

            

Gizmos.DrawRay(startPosition, direction);

Code language: C# ( cs )

The Gizmos.DrawRay method is like to the Gizmos.DrawLine method in which it draws a line.

The difference is the Gizmos.DrawRay method draws a line from the starting position to a specified management for a specified distance.

            

using Organization.Collections; using System.Collections.Generic; using UnityEngine; public class VisualAid : MonoBehaviour { private void OnDrawGizmos() { // Set the color of Gizmos to green Gizmos.color = Color.greenish; Vector2 direction = new Vector2(2.5f, 0); // Draw a line from the position of this object // two.five units to the right Gizmos.DrawRay(transform.position, management); } }

Code language: C# ( cs )

The instance in a higher place draws a light-green debug ray starting from the heart of the game object with a length of 2.5 units to the right.

A ray drawn with Gismoz.DrawRay with a length of 2.5 units and pointing to the right.

This method is useful for determining the range of a Raycast or a projectile that moves in a directly line.

How To Draw A Debug Sphere With Gizmos In Unity

            

Gizmos.DrawSphere(centerPosition, radius); Gizmos.DrawWireSphere(centerPosition, radius);

Code language: C# ( cs )

There are 2 methods for drawing a debug sphere onto the Scene view in Unity Editor.

The Gizmos.DrawSphere method draws a solid sphere at a specified position with a specified radius, whereas the Gizmos.DrawWireSphere method draws a wireframe sphere instead.

            

using System.Collections; using System.Collections.Generic; using UnityEngine; public class VisualAid : MonoBehaviour { private void OnDrawGizmos() { // Set the colour of Gizmos to green Gizmos.color = Colour.green; Gizmos.DrawSphere(transform.position, ane.5f); } }

Code language: C# ( cs )

The example above draws a solid sphere with a radius of 1.5 units positioned at the center of the game object.

A 2D view of the solid sphere drawn by the DrawSphere method.
2D view
A 3D view of the solid sphere drawn by the DrawSphere method.
3D view
            

using System.Collections; using System.Collections.Generic; using UnityEngine; public form VisualAid : MonoBehaviour { private void OnDrawGizmos() { // Set the color of Gizmos to light-green Gizmos.colour = Color.greenish; Gizmos.DrawWireSphere(transform.position, one.5f); } }

Code linguistic communication: C# ( cs )

The example to a higher place draws a wireframe sphere with a radius of 1.v units positioned at the centre of the game object.

A 2D view of the wireframe sphere drawn by the DrawWireSphere method.
2D view
A 3D view of the wireframe sphere drawn by the DrawWireSphere method.
3D view

How To Draw A Debug Cube With Gizmos In Unity

            

Gizmos.DrawCube(centerPosition, radius); Gizmos.DrawWireCube(centerPosition, radius);

Code language: C# ( cs )

There are ii methods for drawing a debug sphere onto the Scene view in Unity Editor.

The Gizmos.DrawCube method draws a solid cube at a specified position with a specified size, whereas the Gizmos.DrawWireCube method draws a wireframe cube instead.

            

using Arrangement.Collections; using System.Collections.Generic; using UnityEngine; public class VisualAid : MonoBehaviour { private void OnDrawGizmos() { // Set up the colour of Gizmos to light-green Gizmos.color = Color.green; Gizmos.DrawCube(transform.position, new Vector3(2f, 1f, 1f)); } }

Code language: C# ( cs )

The example to a higher place draws a solid cube with a size of 2x1x1 positioned at the centre of the game object.

A 2D view of the solid cube drawn by the DrawCube method.
2d view
A 3D view of the solid cube drawn by the DrawWireCube method.
3D view
            

using System.Collections; using Organisation.Collections.Generic; using UnityEngine; public form VisualAid : MonoBehaviour { individual void OnDrawGizmos() { // Ready the color of Gizmos to green Gizmos.color = Color.dark-green; Gizmos.DrawWireCube(transform.position, new Vector3(2f, 1f, 1f)); } }

Lawmaking language: C# ( cs )

The example in a higher place draws a wireframe cube with a size of 2x1x1 positioned at the centre of the game object.

A 2D view of the wireframe sphere drawn by the DrawWireSphere method.
2d view
A 3D view of the wireframe sphere drawn by the DrawWireSphere method.
3D view

Conclusion

Knowing how to use the debugging tools provided by Unity tin can aid you out quite a lot. The Gismoz course is one of those tools you'll find yourself be using often. Especially, if y'all deal with Raycasting or object collisions a lot.

I hope you've learned something new today.

Thanks!

bookerwhows1936.blogspot.com

Source: https://gamedevplanet.com/visual-debugging-in-unity-with-gizmos-draw-methods/

0 Response to "Debug Draw Circle Cast Unity 2d"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel