im trying to create a 2d gui world,i use Nature2d for physics but i want the canvas move something like in terraria so it will look like camera is following the player cube
i tried many thing like putting it on the players position and more but nothing worked
the whole sciprt -
local modus = require(game.ReplicatedStorage.Nature2D)
local rayEngine = require(game.StarterGui:WaitForChild("RayCast2"))
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local viewport = camera.ViewportSize
local World = script.Parent
local Canvas = World.Canvas
local Elements = Canvas.Elements
local engine = modus.init(World)
local UIS = game:GetService("UserInputService")
local Hover
local keys = {
W = false,
A = false,
S = false,
D = false,
Space = false
}
local Player_element = engine:Create("RigidBody",{
Object = Elements.Cube,
Collidable = true,
Anchored = false,
CanRotate = false
})
local Ground_element = engine:Create("RigidBody",{
Object = Elements.Ground,
Collidable = true,
Anchored = true,
Friction = 0
})
local Canvas_element = engine:Create("RigidBody",{
Object = Canvas,
Collidable = false,
Anchored = true,
Friction = 0
})
engine:Start()
local canJump = true
UIS.InputBegan:Connect(function(input, isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.D and not keys.D then
keys.D = true
repeat
wait()
Player_element:ApplyForce(Vector2.new(1,0))
Canvas:TweenPosition(UDim2.new(Canvas.Position.X.Scale + 0.04,0,0,0))
print(Canvas.Position)
until keys.D == false
elseif input.KeyCode == Enum.KeyCode.A then
keys.A = true
repeat
wait()
Player_element:ApplyForce(Vector2.new(-1,0))
Canvas:TweenPosition(UDim2.new(Canvas.Position.X.Scale - 0.04,0,0,0))
print(Canvas.Position)
until keys.A == false
elseif input.KeyCode == Enum.KeyCode.Space then
keys.Space = true
if canJump then
Player_element:ApplyForce(Vector2.new(0,-8))
canJump = false
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.D then
if keys.D then
keys.D = false
end
elseif input.KeyCode == Enum.KeyCode.A then
if keys.A then
keys.A = false
end
elseif input.KeyCode == Enum.KeyCode.Space then
if keys.Space then
keys.Space = false
end
end
end)
game:GetService("RunService").Stepped:Connect(function()
local ray = rayEngine.new(
Vector2.new(Elements.Cube.Position.X.Offset + (Elements.Cube.Size.Width.Offset/2), Elements.Cube.Position.Y.Offset),
Vector2.new(Elements.Cube.Position.X.Offset + (Elements.Cube.Size.Width.Offset), Elements.Cube.Position.Y.Offset + Elements.Cube.Size.Height.Offset)
)
ray.Visible = false
local hit, pointPosition = ray:Cast(World, { Elements.Ground })
if ((pointPosition.Y - ray:GetOrigin().Y) - Elements.Cube.Size.Height.Offset) == 35 then
canJump = true
print((pointPosition.Y - ray:GetOrigin().Y) - Elements.Cube.Size.Height.Offset)
end
end)
i appreaciate any help