Creating a gui 2d world

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)

image

i appreaciate any help

1 Like

Hello, I recommend you to watch this video.

damn this totally was so contributive and helpful to this topic :100:

this is not what he is looking for.

ok

This text is really nice. And I love it.

Did you even read the post?

As for the actual topic, are you get getting any errors?

Elements.Ground.MouseEnter:Connect(function()
Hover = true
end)
Elements.Ground.MouseLeave:Connect(function()
Hover = false
end)

Mouse.Button1Down:Connect(function()
if Hover then
Elements.Ground.BackgroundColor3 = Color3.new(math.random(1,255)/255,math.random(1,255)/255,math.random(1,255)/255)
end
end)

If you want the player to move the camera, simply use the CameraSubject property of the Camera object.

No im not,the problem is i dont know how to put it together because,the world is GUI so i have to move the world (i think)

The whole world and player are gui’s so i cant set it to some part,i tried setting it to the frame that is the player but it desnt seem working

thing is the world is GUI’s so i cant really manipulate with camera