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()
functionTick()ifnotRUN_TESTthenreturnendlocalplayerPos = player:GetWorldPosition()localglobalForward = Vector3.FORWARD * 300localglobalRight = Vector3.RIGHT * 300localglobalUp = Vector3.UP * 300CoreDebug.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 })endCode 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:
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.
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!