Editable image colissions

  1. What do you want to achieve? how to make a collision so that the player cannot pass through the green rectangle? Any tip?

  2. What is the issue? I can go though the “floor” and I need to prevent it.

  3. What solutions have you tried so far? I just have no idea how to do it.

local ImageObject = script.Parent

local EditableImage = Instance.new("EditableImage")
EditableImage.Size = Vector2.one * 1024
EditableImage.Parent = ImageObject

local startPosition : Vector2 = Vector2.new(325, 150)
local updatedPosition : Vector2


EditableImage:DrawRectangle(Vector2.zero, EditableImage.Size, Color3.new(0, 1, 1), 0)
EditableImage:DrawRectangle(Vector2.zero, Vector2.new(EditableImage.Size.X/3, EditableImage.Size.Y), Color3.new(0.137255, 1, 0.0392157), 0)
EditableImage:DrawRectangle(startPosition, Vector2.new(EditableImage.Size.X/7, EditableImage.Size.Y/7), Color3.new(1, 0, 0.0156863), 0)

local KeysBeingPressed = {
	Up = false,
	Left = false,
	Down = false,
	Right = false,
	Space = false
}

local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")

local function DrawPlayer(y, x)
	updatedPosition = startPosition + Vector2.new(y, x)
	EditableImage:DrawRectangle(startPosition, Vector2.new(EditableImage.Size.X/7 + 3, EditableImage.Size.Y/7 + 3), script.Parent.Parent.Color, 1) 
	EditableImage:DrawRectangle(Vector2.zero, EditableImage.Size, Color3.new(0, 1, 1), 0)
	EditableImage:DrawRectangle(Vector2.zero, Vector2.new(EditableImage.Size.X/3, EditableImage.Size.Y), Color3.new(0.137255, 1, 0.0392157), 0)
	startPosition = updatedPosition
	EditableImage:DrawRectangle(updatedPosition, Vector2.new(EditableImage.Size.X/7, EditableImage.Size.Y/7), Color3.new(1, 0, 0.0156863), 0)
end

uis.InputBegan:Connect(function(input : InputObject)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.Up or input.KeyCode == Enum.KeyCode.DPadUp then
		KeysBeingPressed[1] = true
	end
	if input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.Left or input.KeyCode == Enum.KeyCode.DPadLeft then
		KeysBeingPressed[2] = true
	end
	if input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.Down or input.KeyCode == Enum.KeyCode.DPadDown then
		KeysBeingPressed[3] = true
	end
	if input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.Right or input.KeyCode == Enum.KeyCode.DPadRight then
		KeysBeingPressed[4] = true
	end
	if input.KeyCode == Enum.KeyCode.Space then
		KeysBeingPressed[5] = true
	end
end)

uis.InputEnded:Connect(function(input : InputObject)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.Up or input.KeyCode == Enum.KeyCode.DPadUp then
		KeysBeingPressed[1] = false
	end
	if input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.Left or input.KeyCode == Enum.KeyCode.DPadLeft then
		KeysBeingPressed[2] = false
	end
	if input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.Down or input.KeyCode == Enum.KeyCode.DPadDown then
		KeysBeingPressed[3] = false
	end
	if input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.Right or input.KeyCode == Enum.KeyCode.DPadRight then
		KeysBeingPressed[4] = false
	end
	if input.KeyCode == Enum.KeyCode.Space then
		KeysBeingPressed[5] = false
	end
end)
 
local function onPreRender()
	if KeysBeingPressed[1] then 
		DrawPlayer(4, 0)
	end
	if KeysBeingPressed[2] then 
		DrawPlayer(0, -4)
	end
	if KeysBeingPressed[3] then 
		DrawPlayer(-4, 0)
	end
	if KeysBeingPressed[4] then 
		DrawPlayer(0, 4)
	end
	if KeysBeingPressed[5] then
		--???
	end

end

runService.PreRender:Connect(onPreRender)

Clamp the y value.

Code:

local function DrawPlayer(y, x)
	EditableImage:DrawRectangle(startPosition, Vector2.new(EditableImage.Size.X/7 + 3, EditableImage.Size.Y/7 + 3), script.Parent.Parent.Color, 1) 
	EditableImage:DrawRectangle(Vector2.zero, EditableImage.Size, Color3.new(0, 1, 1), 0)
	EditableImage:DrawRectangle(Vector2.zero, Vector2.new(EditableImage.Size.X/3, EditableImage.Size.Y), Color3.new(0.137255, 1, 0.0392157), 0)
	
	updatedPosition = startPosition + Vector2.new(y, x)
	updatedPosition = Vector2.new(updatedPosition.X, math.max(updatedPosition.Y, 100))
	EditableImage:DrawRectangle(updatedPosition, Vector2.new(EditableImage.Size.X/7, EditableImage.Size.Y/7), Color3.new(1, 0, 0.0156863), 0)
	
	startPosition = updatedPosition
end

Modify the 100 until it looks right.

2 Likes

thank you very much, that is working!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.