Using Rotations in Core

Rotations are useful in Core because it takes an object’s facing direction into account.

Overview

To practice using rotations, this demo will be using the player’s world rotation and looking rotation to visualize the different forward vectors. This will visually show what direction the player’s body is facing and the direction the camera view is facing.

Create a New Script

Return to the Escape the Room project in the Core Editor. In the Hierarchy window, expand the Client Context inside the Scripts folder. Create a new script here named RotationsDemo.

Add a Custom Property

Open the Properties window with the RotationsDemo script selected. Add a custom property of type Bool and name it RunTest. This property will control if the script runs so make sure to enable the property.

Display the Global Coordinate Axis

The global coordinate axis is the world’s sense of direction that will never change, similar to the real world’s compass points (north, south, east, west). The CoreDebug namespace has a function to draw a line between two vector positions. As a reminder of how vector addition works, display the global coordinate axis on the player’s position.

Double click the RotationsDemo script to open the Script Editor window. Add the following code:

local RUN_TEST = script:GetCustomProperty("RunTest")

local player = Game.GetLocalPlayer()

function Tick()
   if not RUN_TEST then
      return
   end

   local playerPos = player:GetWorldPosition()

   local globalForward = Vector3.FORWARD * 300
   local globalRight   = Vector3.RIGHT   * 300
   local globalUp      = Vector3.UP      * 300

   CoreDebug.DrawLine(playerPos, playerPos + globalForward,
   {
      color = Color.GREEN,
      thickness = 5
   })
   CoreDebug.DrawLine(playerPos, playerPos + globalRight,
   {
      color = Color.RED,
      thickness = 5
   })
   CoreDebug.DrawLine(playerPos, playerPos + globalUp,
   {
      color = Color.BLUE,
      thickness = 5
   })
end
Code language: JavaScript (javascript)

Preview the Project

Save the script and preview the project. The lines displayed should now follow the player but never rotate because they are using the world’s coordinate directions. The next step will be to use the player’s local coordinate directions.

Display the Player’s Coordinate Axis

To rotate the coordinate axis with the player’s current direction, it will require some simple math. The first step is to get the player’s rotation. The second step is to multiply the global coordinate vectors by the player rotation which returns the player’s coordinate axis.

Open the RotationsDemo script and change the script to look like so:

local RUN_TEST = script:GetCustomProperty("RunTest")

local player = Game.GetLocalPlayer()

function Tick()
   if not RUN_TEST then
      return
   end

   local playerPos = player:GetWorldPosition()

   local globalForward = Vector3.FORWARD * 300
   local globalRight   = Vector3.RIGHT   * 300
   local globalUp      = Vector3.UP      * 300

   local playerRotation = player:GetWorldRotation()
   
   local playerForward = playerRotation * globalForward
   local playerRight   = playerRotation * globalRight
   local playerUp      = playerRotation * globalUp

   CoreDebug.DrawLine(playerPos, playerPos + playerForward,
   {
      color = Color.GREEN,
      thickness = 5
   })
   CoreDebug.DrawLine(playerPos, playerPos + playerRight,
   {
      color = Color.RED,
      thickness = 5
   })
   CoreDebug.DrawLine(playerPos, playerPos + playerUp,
   {
      color = Color.BLUE,
      thickness = 5
   })
end
Code language: JavaScript (javascript)

Preview the Project

Save the script and preview the project. The coordinate vectors should now rotate with the player.

Display the Player’s Looking Direction

The player’s global rotation shows the direction the body is facing but not necessarily the direction the player is looking. There is a separate function inside the Player API for getting the player’s look rotation. Display a line of sight for the player’s look direction.

Open the RotationsDemo script and add the following code to the bottom of the Tick function:

local headPos = playerPos + Vector3.New(0, 0, 100)
local playerLookRotation = player:GetLookWorldRotation()
local playerLookForward = playerLookRotation * Vector3.FORWARD * 3000

CoreDebug.DrawLine(headPos, headPos + playerLookForward,
{
   color = Color.SILVER,
   thickness = 5
})Code language: PHP (php)

Preview the Project

Save the script and preview the project. There should be a silver line projecting forward from the player’s head towards the direction the player is looking.

Cleanup

Open the Properties window with the RotationsDemo script selected. Deactivate the RunTest custom property.

Scroll to Top