Back to wiki
This post is unpublished.
Double check all the info, and if it's ready, click the "publish" button in the post summary.

Ray casts and You

Author:
Zednosius

Category:
Tutorials

Posted:
4 months ago

Revisions:
10 revisions

Last updated:
4 months ago

Ray casts and You

What is Ray casting?

The easiest way of thinking about ray casting, is to imagine it as a laser pointer. You shoot a completely straight ray from the laser pointer, and then you can see where on a surface you have hit.

In the workshop, the ray cast actions all take a start position and an end position. You can see the start position as the position of the laser pointer in the world, and the end position as the position you are aiming at.

To make ray casting more efficient, the ray actually ends completely at end position, even if this position is mid-air.

There are currently three types of ray casts in the workshop. Each has a different behaviour when it comes to return value and default return value.

Ray cast hit position

  • Returns: Position of the intersection between the ray and a surface. (The laser pointer dot.)
  • Default: end position if ray did not intersect any surface.

The hit position ray cast is the most basic of ray casts. It simply returns the position of the laser dot on a surface, or the end position if no surface was hit. Can be used to place effects on places where the player is looking. Image depicting ray cast hit position

Ray cast hit player

  • Returns: The player entity that was hit by the ray.
  • Default: null if ray did not hit any player.

The hit player ray cast returns the player that was hit by the ray. This can be useful if you want to implement a hitscan (no travel time) ability.

Ray cast hit normal

  • Returns: The normal vector of the surface intersection point.
  • Default: Direction Towards(end pos, start pos) (points towards the player.)

The hit normal ray cast returns the normal vector of the surface that was struck.

The normal vector is just a unit-length vector that points straight out from the surface (is orthogonal to the surface). As an example, this could be used for taking into account the angle of approach for a wall jumping rule or implementing a bouncing ball.

Image depicting ray cast hit normal

Common Examples

Ray cast from player camera to reticle.

actions
{
    Set Player Variable(Event Player, R,
    Ray Cast Hit Position(Eye Position(Event Player), 
        Add(Eye Position(Event Player), Multiply(Facing Direction Of(Event Player), 200)),
        All Players(All Teams), Event Player, True));
}

Common Mistakes

  • Only using a direction vector for the end position
    • Fix: Add start position to the direction vector.
  • Using an end position too close to the start position
    • Fix: Lengthen the direction vector by multiplying it with an integer.