Updated Camera Script

This commit is contained in:
OusmBlueNinja 2025-01-03 09:58:58 -06:00
parent f567fb8ee9
commit f6adf1f37d
2 changed files with 100 additions and 52 deletions

View File

@ -1,72 +1,120 @@
local Engine = require("./assets/scripts/engine") local Engine = require("./assets/scripts/engine")
local KeyCode = require("./assets/scripts/keycode") local KeyCode = require("./assets/scripts/keycode")
local transform = nil local transform = nil
local new_rotation = 0 local currentRotation = 0 -- Current Y-axis rotation in degrees
local new_rotationSpeed = 120 local rotationSpeed = 120 -- Degrees per second
local Move_Speec = 600 local moveSpeed = 600 -- Units per second
function OnInit() function OnInit()
local gameObject = Engine.GetGameObjectByTag("Camera") local gameObject = Engine.GetGameObjectByTag("Camera")
transform = gameObject:GetComponent("Transform") if not gameObject then
Engine.Expose("Position X", "nil") Engine.Log("Error: Camera GameObject not found!")
return
end
transform = gameObject:GetComponent("Transform")
if not transform then
Engine.Log("Error: Transform component not found on Camera!")
return
end
-- Initialize currentRotation based on the transform's current rotation
local rotation = transform:GetRotation()
currentRotation = rotation.y
-- Expose initial position and rotation for debugging or UI purposes
Engine.Expose("Position X", rotation.x)
Engine.Expose("Position Y", rotation.y)
Engine.Expose("Rotation Y", currentRotation)
Engine.Log("Init Done") Engine.Log("Init Done")
end end
function OnUpdate(deltaTime) function OnUpdate(deltaTime)
local gameObject = Engine.GetGameObjectByTag("Camera") if not transform then
transform = gameObject:GetComponent("Transform") Engine.Log("Warning: Transform is not initialized.")
return
local old_rotation = transform:GetRotation()
local Position = transform:GetPosition()
if Engine.KeyDown(KeyCode.W) then
Position.x = Position.x + (deltaTime * Move_Speec)
end
if Engine.KeyDown(KeyCode.S) then
Position.x = Position.x - (deltaTime * Move_Speec)
end
if Engine.KeyDown(KeyCode.D) then
Position.z = Position.z + (deltaTime * Move_Speec)
end
if Engine.KeyDown(KeyCode.A) then
Position.z = Position.z - (deltaTime * Move_Speec)
end end
transform:SetPosition(Position) -- Retrieve current rotation and position
local rotation = transform:GetRotation()
Engine.Expose("Position X", Position.x) local position = transform:GetPosition()
Engine.Expose("Position Y", Position.y)
Engine.Expose("Rotation", new_rotation)
local rotation = {
x = old_rotation.x, -- Preserving existing new_rotation on X-axis
y = new_rotation, -- Updated new_rotation on Y-axis for spinning
z = old_rotation.z -- Preserving existing new_rotation on Z-axis
}
-- Apply the new new_rotation to the Transform component
transform:SetRotation(rotation)
-- Update currentRotation based on input
if Engine.KeyDown(KeyCode.Q) then if Engine.KeyDown(KeyCode.Q) then
new_rotation = new_rotation - (deltaTime * new_rotationSpeed) currentRotation = currentRotation - (deltaTime * rotationSpeed)
end end
if Engine.KeyDown(KeyCode.E) then if Engine.KeyDown(KeyCode.E) then
new_rotation = new_rotation + (deltaTime * new_rotationSpeed) currentRotation = currentRotation + (deltaTime * rotationSpeed)
end
-- Normalize rotation to [0, 360) degrees
if currentRotation >= 360 then
currentRotation = currentRotation - 360
elseif currentRotation < 0 then
currentRotation = currentRotation + 360
end end
if new_rotation >= 360 then
new_rotation = 0 -- Apply the updated rotation to the Transform component
local newRotation = {
x = rotation.x, -- Preserving existing rotation on X-axis
y = currentRotation, -- Updated rotation on Y-axis
z = rotation.z -- Preserving existing rotation on Z-axis
}
transform:SetRotation(newRotation)
-- Calculate movement direction based on currentRotation
local rotationRad = math.rad(currentRotation)
-- Forward vector (positive Z direction)
local forward = {
x = math.cos(rotationRad),
z = math.sin(rotationRad)
}
-- Right vector (positive X direction)
local right = {
x = math.sin(rotationRad),
z = -math.cos(rotationRad)
}
-- Initialize movement deltas
local deltaX = 0
local deltaZ = 0
-- Corrected Key-to-Direction Mapping:
-- W/S: Forward/Backward
-- A/D: Left/Right
if Engine.KeyDown(KeyCode.W) then
-- Move forward
deltaX = deltaX + forward.x * moveSpeed * deltaTime
deltaZ = deltaZ + forward.z * moveSpeed * deltaTime
end end
if Engine.KeyDown(KeyCode.S) then
-- Move backward
deltaX = deltaX - forward.x * moveSpeed * deltaTime
deltaZ = deltaZ - forward.z * moveSpeed * deltaTime
end
if Engine.KeyDown(KeyCode.A) then
-- Move right
deltaX = deltaX + right.x * moveSpeed * deltaTime
deltaZ = deltaZ + right.z * moveSpeed * deltaTime
end
if Engine.KeyDown(KeyCode.D) then
-- Move left
deltaX = deltaX - right.x * moveSpeed * deltaTime
deltaZ = deltaZ - right.z * moveSpeed * deltaTime
end
-- Update position based on calculated deltas
position.x = position.x + deltaX
position.z = position.z + deltaZ
transform:SetPosition(position)
-- Expose updated position and rotation for debugging or UI purposes
Engine.Expose("Position X", position.x)
Engine.Expose("Position Y", position.y)
Engine.Expose("Rotation Y", currentRotation)
end end

View File

@ -152,10 +152,10 @@ Collapsed=0
DockId=0x00000022,0 DockId=0x00000022,0
[Window][ Profiler] [Window][ Profiler]
Pos=345,28 Pos=345,846
Size=1189,816 Size=1189,323
Collapsed=0 Collapsed=0
DockId=0x0000001F,1 DockId=0x00000025,1
[Table][0xE9E836E4,4] [Table][0xE9E836E4,4]
Column 0 Weight=1.2999 Column 0 Weight=1.2999
@ -193,7 +193,7 @@ DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,51 Size=
DockNode ID=0x00000017 Parent=0x0000000D SizeRef=1202,776 Split=Y Selected=0xDFF75B3F DockNode ID=0x00000017 Parent=0x0000000D SizeRef=1202,776 Split=Y Selected=0xDFF75B3F
DockNode ID=0x0000001D Parent=0x00000017 SizeRef=518,720 Split=Y Selected=0x9A7B23B9 DockNode ID=0x0000001D Parent=0x00000017 SizeRef=518,720 Split=Y Selected=0x9A7B23B9
DockNode ID=0x0000001F Parent=0x0000001D SizeRef=549,359 CentralNode=1 Selected=0x9A7B23B9 DockNode ID=0x0000001F Parent=0x0000001D SizeRef=549,359 CentralNode=1 Selected=0x9A7B23B9
DockNode ID=0x00000025 Parent=0x0000001D SizeRef=549,323 Selected=0x1F29F1F5 DockNode ID=0x00000025 Parent=0x0000001D SizeRef=549,323 Selected=0x7A66B86B
DockNode ID=0x0000001E Parent=0x00000017 SizeRef=518,417 Selected=0xC74E1AEE DockNode ID=0x0000001E Parent=0x00000017 SizeRef=518,417 Selected=0xC74E1AEE
DockNode ID=0x00000018 Parent=0x0000000D SizeRef=1202,364 Split=X Selected=0x1C0788A1 DockNode ID=0x00000018 Parent=0x0000000D SizeRef=1202,364 Split=X Selected=0x1C0788A1
DockNode ID=0x00000019 Parent=0x00000018 SizeRef=601,364 Selected=0x1C0788A1 DockNode ID=0x00000019 Parent=0x00000018 SizeRef=601,364 Selected=0x1C0788A1