Vector3 Math for Precise UEFN Object Positioning
Tutorial intermediate

Vector3 Math for Precise UEFN Object Positioning

Updated intermediate

What you'll learn

In this lesson you will build a vector3 from X, Y, Z components, add it to a device's world position to compute a target point, and surface that math on screen using the real Player UI system (GetPlayerUI[] + a text_block added to a canvas).

How it works

A vector3 in Verse holds X, Y, and Z float components (the SpatialMath type used by devices and characters). To offset a position you simply add two vectors: NewPosition := BasePosition + Offset. Verse supports + and - directly on vector3, so A + B returns a new vector3 whose components are added pairwise.

To see the result at runtime we use the Player UI. player.GetPlayerUI[] is a fallible call (note the []) that returns a player_ui. We then build a text_block widget, set its text with string interpolation, wrap it in a canvas slot, and call AddWidget. Because indexing an array (GetPlayers()[0]) and GetPlayerUI[] can both fail, we guard them inside if conditions.

Let's build it

  1. Place a Creative Device in your level.
  2. Create and attach this Verse script, then build Verse code.
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /UnrealEngine.com/Temporary/UI }
using { /Fortnite.com/UI }

# Demonstrates vector3 arithmetic: compute a target position from the
# device's origin + an editable offset, then show it on the player HUD.
vector3_positioning_device := class<concrete>(creative_device):

    # Editable offset (in cm): 500 units "forward" (X) and 200 up (Z).
    @editable
    Offset : vector3 = vector3{ X := 500.0, Y := 0.0, Z := 200.0 }

    OnBegin<override>()<suspends>: void =
        # 1. Read this device's current world position from its transform.
        BasePosition := GetTransform().Translation

        # 2. Vector3 arithmetic: add the offset to the base position.
        #    '+' on two vector3 values adds X, Y, Z component-wise.
        TargetPosition := BasePosition + Offset

        # Log the math so it shows in the output log as well.
        Print("Base: {BasePosition.X}, {BasePosition.Y}, {BasePosition.Z}")
        Print("Target: {TargetPosition.X}, {TargetPosition.Y}, {TargetPosition.Z}")

        # 3. Display the calculated target on each player's HUD.
        for (Player : GetPlayspace().GetPlayers()):
            # GetPlayerUI[] is fallible, so guard it inside an if.
            if (PlayerUI := GetPlayerUI[Player]):
                # Build the readable label with string interpolation.
                Label := "Target: {TargetPosition.X}, {TargetPosition.Y}, {TargetPosition.Z}"
                # Create a text widget and set its content.
                TextWidget := text_block{ DefaultText := "initializing..." }
                TextWidget.SetText(Label)
                # Wrap the text in a canvas so it can be added to the HUD.
                HudCanvas := canvas:
                    Slots := array:
                        canvas_slot:
                            Anchors := anchors{ Minimum := vector2{ X := 0.5, Y := 0.1 }, Maximum := vector2{ X := 0.5, Y := 0.1 } }
                            Offsets := margin{ Top := 0.0, Left := 0.0, Right := 0.0, Bottom := 0.0 }
                            Alignment := vector2{ X := 0.5, Y := 0.0 }
                            SizeToContent := true
                            Widget := TextWidget
                # AddWidget renders the canvas on this player's screen.
                PlayerUI.AddWidget(HudCanvas)

Try it yourself

  • Change Offset in the device's Details panel and re-run to watch the target update.
  • Swap BasePosition + Offset for BasePosition - Offset to move the target behind/below the device.
  • Read a player's GetFortCharacter[].GetViewLocation() and offset from that instead of the device origin, so the target follows where the player is aiming.

Recap

  • A vector3 has X, Y, and Z float components in SpatialMath.
  • + and - operate component-wise on vectors: A + B returns a new vector3.
  • GetTransform().Translation gives a device's world position.
  • GetPlayerUI[Player] is fallible — guard it with if, then AddWidget a canvas containing a text_block to render debug info on the HUD.

Check your understanding

Test yourself with an interactive quiz and track your progress + earn XP — free for members.

Turn this into a guided course

Add Vector3 Arithmetic for Object Positioning to your free study plan — we'll suggest related pages and stitch the lot into one compile-checked, self-guided lesson with worked examples and quizzes.

Original tutorial generated by Verse Island from the Verse/UEFN knowledge base, with references to the Epic Games sources above. Code is validated against the knowledge base.

Comments

    Sign in to vote, comment, or suggest an edit. Sign in