Skip to content
HitResult

Utils / HitResult

HitResult Class

The hit result contains information about a hit of the track, such as the impact point and the surface normal of the point.

Usage example: create a script named "HitResultExample", place it in the object bar, open the script, enter the following code to save, and create a model in the scene, place it in the position of 200, 10, 0, run the game, and you will see the HitResult information array detected by ray detect in the log. The code is as follows:

ts
const result = QueryUtil.lineTrace(new Vector(100), new Vector(1000), true, true);
@Component
export default class HitResultExample extends Script {

    protected onStart(): void {
        if (this.isRunningClient()) {
            // Start position
            let startLocation = new Vector(100, 10, 100);
            // End position
            let endLocation = new Vector(1000, 10, 100);
            // Returned HitResult array
            const result = QueryUtil.lineTrace(startLocation, endLocation, true, true);
            result.forEach(element => {
                // Accessing the name of the return value gameObject through HitResult
                Console.log (name of GameObject hit: ${element. gameObject. name} `);
            });
        }
    }
}
const result = QueryUtil.lineTrace(new Vector(100), new Vector(1000), true, true);
@Component
export default class HitResultExample extends Script {

    protected onStart(): void {
        if (this.isRunningClient()) {
            // Start position
            let startLocation = new Vector(100, 10, 100);
            // End position
            let endLocation = new Vector(1000, 10, 100);
            // Returned HitResult array
            const result = QueryUtil.lineTrace(startLocation, endLocation, true, true);
            result.forEach(element => {
                // Accessing the name of the return value gameObject through HitResult
                Console.log (name of GameObject hit: ${element. gameObject. name} `);
            });
        }
    }
}

Table of contents

Properties

blockingHit: boolean
Whether the object is hit. If there is a collision, it is true; otherwise, it is false
boneName: string
Name of colliding bones
distance: number
Distance, the distance from traceStart to location
gameObject: GameObject
Hit GameObject
impactNormal: Vector
The normal of the object (if any) that was hit by the sweep in world space. For example, if the sphere hits a plane, this is a normalized vector pointing from the plane. In the case of collision with corners or edges of a surface, the "most opposite" normal (opposite to the query direction) is usually selected.
impactPoint: Vector
The position in world space of the actual contact between the track shape (box, sphere, light, etc.) and the affected object. Example: For sphere tracking testing, this is the point where the surface of the sphere contacts other objects.
normal: Vector
The normal of the swept object in world space. Equivalent to ImpactNormal for line testing. This is calculated for capsules and spheres, otherwise it will be the same as ImpactNormal. Example: For sphere trajectory testing, this is the normalized vector pointing to the center of the sphere at the impact point.
position: Vector
The position in world space, if a collision occurs, the moving shape will eventually come into contact with the affected object. Equivalent to the impact point of circuit testing. Example: For sphere tracking testing, this is the point where the center of the sphere is located when it contacts other objects. For sweep movements (but not queries), this may not be equal to the final position of the shape, as hits will be slightly pulled back to prevent accuracy issues from overlapping with another surface.
time: number
This is the hit time along the tracking direction, ranging from [0.0 to 1.0]. If not hit, it will return 1.0.
traceEnd: Vector
End position of collision detect
traceStart: Vector
Starting point position for collision detection

Properties

blockingHit

blockingHit: boolean

Whether the object is hit. If there is a collision, it is true; otherwise, it is false


boneName

boneName: string

Name of colliding bones


distance

distance: number

Distance, the distance from traceStart to location


gameObject

gameObject: GameObject

Hit GameObject


impactNormal

impactNormal: Vector

The normal of the object (if any) that was hit by the sweep in world space. For example, if the sphere hits a plane, this is a normalized vector pointing from the plane. In the case of collision with corners or edges of a surface, the "most opposite" normal (opposite to the query direction) is usually selected.


impactPoint

impactPoint: Vector

The position in world space of the actual contact between the track shape (box, sphere, light, etc.) and the affected object. Example: For sphere tracking testing, this is the point where the surface of the sphere contacts other objects.


normal

normal: Vector

The normal of the swept object in world space. Equivalent to ImpactNormal for line testing. This is calculated for capsules and spheres, otherwise it will be the same as ImpactNormal. Example: For sphere trajectory testing, this is the normalized vector pointing to the center of the sphere at the impact point.


position

position: Vector

The position in world space, if a collision occurs, the moving shape will eventually come into contact with the affected object. Equivalent to the impact point of circuit testing. Example: For sphere tracking testing, this is the point where the center of the sphere is located when it contacts other objects. For sweep movements (but not queries), this may not be equal to the final position of the shape, as hits will be slightly pulled back to prevent accuracy issues from overlapping with another surface.


time

time: number

This is the hit time along the tracking direction, ranging from [0.0 to 1.0]. If not hit, it will return 1.0.


traceEnd

traceEnd: Vector

End position of collision detect


traceStart

traceStart: Vector

Starting point position for collision detection