Skip to content

Game.Common.RaycastHit

Assembly:
Assembly-CSharp (game code)

Namespace: Game.Common

Type: struct

Base: Implements System.IEquatable

Summary: Represents the result of a raycast in the game. Contains the entity hit (if any), several position and direction vectors, a grid/cell index, and auxiliary distance/curve information. Implemented as a value type (struct) and provides IEquatable for value equality and a corresponding GetHashCode implementation.


Fields

  • public Unity.Entities.Entity m_HitEntity
    Entity that was hit by the raycast. This is the Unity.Entities.Entity identifier for the hit object (may be Entity.Null if nothing specific was hit).

  • public Unity.Mathematics.float3 m_Position
    The origin position of the raycast (or a related reference position). Represents a 3D point using Unity.Mathematics.float3.

  • public Unity.Mathematics.float3 m_HitPosition
    The world position where the ray actually intersected geometry (the hit point).

  • public Unity.Mathematics.float3 m_HitDirection
    The direction vector of the incoming ray at the hit point.

  • public Unity.Mathematics.int2 m_CellIndex
    An integer 2D grid/cell index associated with the hit (e.g., map/tile cell coordinates).

  • public System.Single m_NormalizedDistance
    A normalized distance value (float). Typically used to represent how far along a segment or ray the hit occurred (value interpretation depends on caller).

  • public System.Single m_CurvePosition
    Auxiliary float value often used for curve/spline position information when raycasting against curved geometry or splines.

Properties

  • None (no properties defined; public fields only)

Constructors

  • public RaycastHit() (implicit default struct constructor)
    Because RaycastHit is a struct, it has an implicit parameterless constructor that zero-initializes fields. There are no explicit constructors defined in the source.

Methods

  • public bool Equals(RaycastHit other)
    Compares this instance to another RaycastHit for equality. The implementation performs a field-by-field comparison:
  • Uses Entity.Equals for m_HitEntity
  • Uses float3.Equals for m_Position, m_HitPosition, and m_HitDirection
  • Uses int2.Equals for m_CellIndex
  • Compares m_NormalizedDistance and m_CurvePosition using exact float equality (==)

Notes: - The equality uses exact floating-point equality. For values produced by floating-point calculations, consider using an epsilon/approximate comparison if appropriate for your use case. - This method implements IEquatable which is used by generic equality comparers (e.g., EqualityComparer.Default).

  • public override int GetHashCode()
    Returns a combined hash code computed from all fields. The implementation multiplies and adds field hash codes in a typical pattern: ((((((17 * 31 + m_HitEntity.GetHashCode()) * 31 + m_Position.GetHashCode()) * 31 + m_HitPosition.GetHashCode()) * 31 + m_HitDirection.GetHashCode()) * 31 + m_CellIndex.GetHashCode()) * 31 + m_NormalizedDistance.GetHashCode()) * 31 + m_CurvePosition.GetHashCode();

Notes: - The hash code uses the hash codes of the float fields and value types; changes in floating-point values will affect the hash. - Because Equals(RaycastHit) is provided but Equals(object) is not overridden, rely on EqualityComparer.Default where appropriate (it will use IEquatable).

Usage Example

using Unity.Entities;
using Unity.Mathematics;
using Game.Common;

// create a hit record
RaycastHit hit = new RaycastHit {
    m_HitEntity = someEntity,
    m_Position = new float3(0f, 10f, 0f),
    m_HitPosition = new float3(0f, 0f, 0f),
    m_HitDirection = math.normalize(new float3(0f, -1f, 0f)),
    m_CellIndex = new int2(12, 34),
    m_NormalizedDistance = 0.5f,
    m_CurvePosition = 0f
};

// compare two hits
RaycastHit other = /* obtained elsewhere */;
bool equal = hit.Equals(other);

// use in a collection (EqualityComparer<RaycastHit>.Default honors IEquatable<T>)
var set = new System.Collections.Generic.HashSet<RaycastHit>();
set.Add(hit);

Additional notes: - Types used: Unity.Entities.Entity (Entity identifier), Unity.Mathematics.float3/int2 (math types from Unity.Mathematics). - Because this is a struct, instances are copied by value. Be mindful when passing this type around in performance-sensitive code to avoid unnecessary copies. - If you need fuzzy comparisons for physical proximity or directional similarity, implement your own comparison using tolerances rather than relying on the struct's Equals implementation.