/RaycastVisualization

This asset allows users to view raycasts as the user fires them.

Primary LanguageC#

Banner

This asset allows users to view raycasts as the user fires them.

Supports both the 2D and 3D api.

GIF Examples Of All Visuals

3D API (click to expand)

Raycast / Linecast

raycast

RaycastAll / RaycastNonAlloc

raycast_all

CapsuleCast

capsulecast

CapsuleCastAll / CapsuleCastNonAlloc

capsulecast_all

CheckCapsule

check_capsule

OverlapCapsule / OverlapCapsuleNonAlloc

overlap_capsule

BoxCast

boxcast

BoxCastAll / BoxCastNonAlloc

boxcast_all

CheckBox

check_box

OverlapBox / OverlapBoxNonAlloc

overlap_box

SphereCast

spherecast

SphereCastAll / SphereCastNonAlloc

spherecast_all

CheckSphere

check_sphere

OverlapSphere / OverlapSphereNonAlloc

overlap_sphere

Compute Penetration

compute_penetration

Closest Point

closest_point
2D API (click to expand)

Raycast

raycast

RaycastAll / RaycastAll / RaycastNonAlloc

raycast_all

CapsuleCast

capsulecast

CapsuleCastAll / CapsuleCastAll / CapsuleCastNonAlloc

capsulecast_all

OverlapCapsule

overlap_capsule

OverlapCapsuleAll / OverlapCapsuleNonAlloc

overlap_capsule_all

BoxCast

boxcast

BoxCastAll / BoxCastAll / BoxCastNonAlloc

boxcast_all

OverlapBox

overlap_box

OverlapBoxAll / OverlapBoxNonAlloc

overlap_box_all

CircleCast

circlecast

CircleCastAll / CircleCastAll / CircleCastNonAlloc

circlecast_all

OverlapCircle

overlap_circle

OverlapCircleAll / OverlapCircleNonAlloc

overlap_circle_all

OverlapPoint

overlap_point

OverlapPointAll / OverlapPointNonAlloc

overlap_point_all

OverlapArea

overlap_area

OverlapAreaAll / OverlapAreaNonAlloc

overlap_area_all

OverlapCollider

overlap_collider

Closest Point

closest_point

Distance

distance

GetContacts

get_contacts

GetContacts (points)

get_contacts_points

IsTouching

is_touching

IsTouchingLayers

is_touching_layers

GetRayIntersection

get_ray_intersection

GetRayIntersectionAll / GetRayIntersectionNonAlloc

get_ray_intersection_all

Installation

Using Unity Package Manager

  1. Open the Package Manager from Window/Package Manager
  2. Click the '+' button in the top-left of the window
  3. Click 'Add package from git URL'
  4. Provide the URL of this git repository: https://github.com/nomnomab/RaycastVisualization.git
  5. Click the 'add' button

Usage

To get a visual to show up for a physics call simply do the following:

For 3D:

  • Replace Physics. with VisualPhysics..

For 2D:

  • Replace Physics2D. with VisualPhysics2D..
  • Some 2D functions rely more on a 3D perspective in the editor depending on the orientation of the casts.
// Example
void SomeFunction() {
    if (VisualPhysics.Raycast(position, direction)) {
        Debug.Log("Hit!");
    }
}

API Switching:

You can also use a trick to automatically swap between the two APIs (useful for when you want to use the visual API in the editor, but the normal API in builds):

  • Using VisualPhysics in a build will use the normal Physics API, however the method call may not be inlined depending on the compiler's mood.
#if UNITY_EDITOR
using Physics = Nomnom.RaycastVisualization.VisualPhysics;
#else
using Physics = UnityEngine.Physics;
#endif

void SomeFunction() {
    if (Physics.Raycast(position, direction)) {
        Debug.Log("Hit!");
    }
}

Defining a Visual's Lifetime:

Using VisualLifetime.Create(seconds) you can define how long a cast will display for:

// will display the raycast for a second, rather than a single frame
using (VisualLifetime.Create(1f)) {
    if (VisualPhysics.Raycast(position, direction)) {
        Debug.Log("Hit");
    }
}

User Options

The user options are located under Edit/Preferences/RaycastVisualization

Settings