The third puzzle room will be a mix of quaternions, rotations, and vectors. The main new concept will be using a quaternion to find a specific rotation by applying a vector to another vector.
Puzzle Overview
The third puzzle riddle is as follows:
Karl is too distracted in his hobbies if only his love life could align
This means the player must move the soccer ball so Karl is looking at Kate.
Create a Server Script
In the Hierarchy, add a new script named Puzzle3Logic to the Server Context within the Scripts folder.
Add Custom Properties
This puzzle is a bit different because the player does not necessarily matter. It only cares if the soccer ball is in the correct alignment so the two statues face each other.
Expand the Escape The Room Template and find the Puzzle 3 group. This will contain the objects required by the script. Then open the Properties window with the Puzzle3Logic script selected.
Add the following custom properties:
From the Project Content window, drag and drop the PuzzleAPI script as a custom property.
Drag and drop the Statue 1 object as a custom property.
Drag and drop the Statue 2 object as a custom property.
Drag and drop the Ball object as a custom property.
Using the Dot Product
The Dot Product was introduced in the second puzzle room and can be used to check if an object is facing a certain direction. This will be useful to check if the Karl statue is currently facing the ball or Kate statue. Our API script has a function GetFacingDotProduct that takes in two objects and returns a value between -1 (facing opposite direction) and 1 (facing exact direction) to represent how much the first object is facing the second object. The script will make Karl rotate to face the ball until it is looking at the Kate statue.
Open the Puzzle3Logic script. Add this code:
local API = require(script:GetCustomProperty("PuzzleAPI"))
local STATUE_1 = script:GetCustomProperty("Statue1"):WaitForObject()
local STATUE_2 = script:GetCustomProperty("Statue2"):WaitForObject()
local BALL = script:GetCustomProperty("Ball"):WaitForObject()
local rotateSpeed = 0.5
local stopDP = 0.999
local functionRotateStatue()
localballDP = API.GetFacingDotProduct(STATUE_1, BALL)
localtargetDP = API.GetFacingDotProduct(STATUE_1, STATUE_2)
iftargetDP > stopDPthenSTATUE_1:StopRotate()
Events.Broadcast("OpenPuzzleDoor", "3")
elseifballDP <= stopDPthenSTATUE_1:LookAtContinuous(BALL, true, rotateSpeed)
elseSTATUE_1:StopRotate()
endendfunctionTick()
RotateStatue()
endCode language:JavaScript(javascript)
Preview the Project
Moving the ball should make Karl rotate to follow. Once Karl is looking at Kate, the puzzle door should open.
One Slight Issue
The puzzle room is functioning but there is a slight issue. Karl is not looking exactly at Kate and is off by a small degree amount. This can be seen in the properties values of the rotation.
Using Quaternions
There are many ways to find the exact rotation required for an object to face another object. The following logic will be projecting the two object’s offset vector onto a forward vector by creating a Quaternion.
Open the Puzzle3Logic script. Add this code:
local API = require(script:GetCustomProperty("PuzzleAPI"))
local STATUE_1 = script:GetCustomProperty("Statue1"):WaitForObject()
local STATUE_2 = script:GetCustomProperty("Statue2"):WaitForObject()
local BALL = script:GetCustomProperty("Ball"):WaitForObject()
local rotateSpeed = 0.5local stopDP = 0.999local functionRotateStatue()localballDP = API.GetFacingDotProduct(STATUE_1, BALL)localtargetDP = API.GetFacingDotProduct(STATUE_1, STATUE_2)localoffset = API.GetOffsetVector(STATUE_1, STATUE_2)localtargetQuat = Quaternion.New(Vector3.FORWARD, Vector3.New(offset, 0))localtargetRot = targetQuat:GetRotation()iftargetDP > stopDPthenSTATUE_1:StopRotate()STATUE_1:SetWorldRotation(targetRot)Events.Broadcast("OpenPuzzleDoor", "3")elseifballDP <= stopDPthenSTATUE_1:LookAtContinuous(BALL, true, rotateSpeed)elseSTATUE_1:StopRotate()endendfunctionTick()RotateStatue()endCode language:PHP(php)
Preview the Project
The Karl statue should now snap to the exact rotation to face Kate head on.
Summary
The Escape the Room project is now complete! There are many different applications for Vectors, Rotations, and Quaternions. This project highlighted just a few of them.
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!