Using Vectors in Core

Now that basic vector information has been covered, it’s time to put them to use in Core. Return to the Core Editor and open up the Escape the Room project.

Overview

This demo will show how to access the player position and apply vector math to the position vector.

Create a New Script

In the Hierarchy, create a Client Context to the Scripts folder. Then create a new script named VectorsDemo and place it in the Client Context.

Add a Custom Property

Open the Properties window with the VectorsDemo script selected. Add a new custom property of the type Bool and name it RunTest. This property will run the code if active, so make sure to activate the property once it is created.

Player Position Vector

From a client script, the Game namespace has a function to get the local player object. A Player object has a function to access the position vector. The CoreDebug namespace can then visually display a sphere at a given position. To constantly redraw the sphere at the player’s position, a Tick function will keep the logic going forever.

Open up the VectorsDemo script. Add the following code to display a circle at the player’s position.

local RUN_TEST = script:GetCustomProperty("RunTest") local player = Game.GetLocalPlayer() function Tick() if not RUN_TEST then return end local playerPosition = player:GetWorldPosition() CoreDebug.DrawSphere(playerPosition, 20, {color = Color.YELLOW}) end
Code language: Lua (lua)

Preview the Project

The yellow sphere should now follow the player.

The player’s position is the center point of the body.

The Head Position

The player’s head position can be calculated by adding an upward vector to the player’s midpoint position. A player is roughly 2 meters tall in Core, so 100 (centimeters) needs to be added to the z component.

Add the following code right after drawing the yellow debug sphere inside the Tick function:

local upwardVector = Vector3.New(0, 0, 100) local headPosition = playerPosition + upwardVector CoreDebug.DrawSphere(headPosition, 20, {color = Color.BLUE})
Code language: Lua (lua)

Preview the Project

There should now be a blue sphere at the player’s head position.

Global Space

The world coordinates are constant directions independent from other objects. This is known as global space. For example, if the player was rotated, then the blue sphere would not be on the head anymore. This is because the upward vector is in global space and does not care about the player’s rotation.

North Star

To demonstrate global space even further, let’s add a sphere that is always “north” of the player. North is the global space forward direction that will never change.

Add the following code right after drawing the blue debug sphere inside the Tick function:

local forwardVector = Vector3.FORWARD * 100 local northStarPosition = playerPosition + forwardVector CoreDebug.DrawSphere(northStarPosition, 20, {color = Color.GREEN})
Code language: Lua (lua)

Preview the Project

There should be a green sphere always north of the player.

East Star

The last thing for this demo is to add a sphere to the east of the player. Add the following code after drawing the north sphere:

local eastStarPosition = playerPosition + Vector3.RIGHT * 100 CoreDebug.DrawSphere(eastStarPosition, 20, {color = Color.RED})
Code language: Lua (lua)

Order of Operations

For equations with multiple operators, the order of operations is the same as regular math. So the first line of code will multiply the vector by the number before the vector addition.

Preview the Project

There should now be a red sphere to the east of the player.

Cleanup

Select the VectorsDemo script and open the Properties window. Deactivate the RunTest property so the debug spheres do not appear anymore.

Scroll to Top