Files
sampleproj/Assets/lua/movable.lua
2026-03-29 13:18:13 +02:00

53 lines
1.8 KiB
Lua

-- movable.lua - Object that teleports on button press (event-driven)
-- Tests: onButtonPress, Entity.SetPosition, Vec3 math, Camera.GetPosition,
-- Input constants, self.position, self.rotationY
local selected = false
function onCreate(self)
selected = false
Debug.Log("Movable object created at "
.. self.position.x .. "," .. self.position.y .. "," .. self.position.z)
end
function onInteract(self)
selected = not selected
if selected then
setStatus("Object selected! D-pad to move, Triangle to deselect.")
else
setStatus("Object deselected.")
end
end
function onButtonPress(self, button)
if not selected then return end
local pos = self.position
local step = 1
if button == Input.UP then
Entity.SetPosition(self, Vec3.new(pos.x, pos.y, pos.z + step))
elseif button == Input.DOWN then
Entity.SetPosition(self, Vec3.new(pos.x, pos.y, pos.z - step))
elseif button == Input.LEFT then
Entity.SetPosition(self, Vec3.new(pos.x - step, pos.y, pos.z))
elseif button == Input.RIGHT then
Entity.SetPosition(self, Vec3.new(pos.x + step, pos.y, pos.z))
elseif button == Input.L1 then
Entity.SetPosition(self, Vec3.new(pos.x, pos.y + step, pos.z))
elseif button == Input.R1 then
Entity.SetPosition(self, Vec3.new(pos.x, pos.y - step, pos.z))
elseif button == Input.TRIANGLE then
selected = false
setStatus("Object deselected.")
elseif button == Input.SQUARE then
local rot = Entity.GetRotationY(self)
Entity.SetRotationY(self, rot + 1/2)
end
local camPos = Camera.GetPosition()
local diff = Vec3.sub(self.position, camPos)
local dist = Vec3.length(diff)
Debug.Log("Movable moved. Dist to camera: " .. dist)
end