Quaternions allow more precise control of rotation and checking an object’s direction.
Overview
To practice using quaternions, this lesson will make a visual demonstration of a vector being rotated from one direction to another. This will be utilizing the quaternion’s slerp function.
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 QuaternionsDemo.
Add a Custom Property
Open the Properties window with the QuaternionsDemo 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 a Front and Back Position
For this demonstration, the vector will spin around the player starting in the forward looking direction and ending in the backward looking direction. Start by creating position vectors for the starting and ending point of the vector.
Double click the QuaternionsDemo script to open the Script Editor window. Add the following code:
local RUN_TEST = script:GetCustomProperty("RunTest")
local player = Game.GetLocalPlayer()
if not RUN_TEST then
return
end
local playerPos = player:GetWorldPosition()
local playerRot = player:GetWorldRotation()
local frontPos = playerPos + (playerRot * Vector3.FORWARD * 300)
local behindPos = playerPos + (playerRot * Vector3.FORWARD * -300)
local steps = 1500for i = 1, steps, 1do
CoreDebug.DrawSphere(frontPos, 20, {color = Color.GREEN})
CoreDebug.DrawSphere(behindPos, 20, {color = Color.RED})
endCode language:JavaScript(javascript)
Preview the Project
Save the script and press the Play button to preview the project. There should be a sphere being drawn in front and behind the player.
Add a Quaternion Slerp
Now it’s time to create two quaternions (front and back facing) and have a vector transition (or slerp) from one to the other. You may have noticed a for loop in the previous code counting steps. This will control the speed of the transition.
Add the following code to the QuaternionsDemo script:
local RUN_TEST = script:GetCustomProperty("RunTest")
local player = Game.GetLocalPlayer()
if not RUN_TEST then
returnend
local playerPos = player:GetWorldPosition()
local playerRot = player:GetWorldRotation()
local frontPos = playerPos + (playerRot * Vector3.FORWARD * 300)
local behindPos = playerPos + (playerRot * Vector3.FORWARD * -300)
--Create quaternion using a player's rotationlocal startQuaternion = Quaternion.New(playerRot)--Create quaternion using two positionslocal endQuaternion = Quaternion.New(playerPos, behindPos)local steps = 1500for i = 1, steps, 1 do CoreDebug.DrawSphere(frontPos, 20, {color = Color.GREEN}) CoreDebug.DrawSphere(behindPos, 20, {color = Color.RED}) --Quaternion transition visualized by yellow line local currentQuat = Quaternion.Slerp(startQuaternion, endQuaternion, i/steps) local currentForwardVector = currentQuat:GetForwardVector() * 300 CoreDebug.DrawLine(playerPos, playerPos + currentForwardVector, { color = Color.YELLOW, thickness = 5 }) --Make sure to wait a frame so line can be animated Task.Wait()end Code language:PHP(php)
Preview the Project
Save the script and preview the project. There should now be a yellow line transitioning from the front of the player to behind the player.
Cleanup
Open the Properties window with the QuaternionsDemo 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!