How could I prevent this weird collision from happening

Hi Ted here. I’m making a pong game and I have this weird problem where this brick is going inside of this other brick at a specific angle and is there any idea how maybe I could script something around it

https://gyazo.com/90e4598a1e43723b7f8a034b217cd31e

Code:

local redpaddle = game.Workspace:WaitForChild('RedPaddle');
local bluepaddle = game.Workspace:WaitForChild('BluePaddle');
local part = game.Workspace:WaitForChild('Part');
local Baseplate = game.Workspace.Baseplate;

local StartX = Baseplate.Position.X-Baseplate.Size.X/2
local StartY = Baseplate.Position.Y-Baseplate.Size.Y/2
local StartZ = Baseplate.Position.Z-Baseplate.Size.Z/2


local CanvasSizeZ = Baseplate.Size.Z
local CanvasSizeX = Baseplate.Size.X

local Position = part.Position+Vector3.new(0,0,0)
local Velocity = Vector3.new(-1,0,-1)
local Speed = 0.1

local UserInputService = game:GetService('UserInputService')

--need a start on the x axis 

game:GetService('RunService').RenderStepped:Connect(function()
	Position+=Velocity*Speed -- update postion
	part:PivotTo(CFrame.new(Position))
	if (part.Position.Z+part.Size.Z/2 >= StartZ+CanvasSizeZ or part.Position.Z-part.Size.Z/2 <= StartZ) then
		Velocity = Vector3.new(Velocity.X, 0, -Velocity.Z)
	end
	if (part.Position.X+part.Size.X/2 >= StartX+CanvasSizeX or part.Position.X-part.Size.X/2 <= StartX) then
		Velocity = Vector3.new(-Velocity.X, 0, Velocity.Z)
	end
	
	if (part.Position.X >= 8.4) then
		if part.Position.Z > (bluepaddle.Position.Z - 2.5) and part.Position.Z < (bluepaddle.Position.Z + 2.5) then
			--print(part.Position.Z)
			Velocity = Vector3.new(-Velocity.X, 0, Velocity.Z)
		end
		if part.Position.Z >= (bluepaddle.Position.Z + 2.5) and part.Position.Z < (bluepaddle.Position.Z + 2.5) then
			Velocity = Vector3.new(-Velocity.X, 0, Velocity.Z)
		end
		if part.Position.Z >= -16.05 and bluepaddle.Position.Z > part.Position.Z and part.Position.X >= 8.4 then
			Velocity = Vector3.new(-Velocity.X, 0, Velocity.Z)
		end
	end
	
	if (part.Position.X <= -4.35) then
		--print('db')
		if part.Position.Z > (redpaddle.Position.Z - 2.5) and part.Position.Z < (redpaddle.Position.Z + 2.5) then
			--print('db1')
			Velocity = Vector3.new(-Velocity.X, 0, Velocity.Z)
		end
		
	end
end)

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.F then
			bluepaddle.CFrame = bluepaddle.CFrame + Vector3.new(0, 0, 0.4)
		end
		if input.KeyCode == Enum.KeyCode.R then
			bluepaddle.CFrame = bluepaddle.CFrame + Vector3.new(0, 0, -0.4)
		end
	end
end)

Please post your code. We can’t do much to help you like this.

2 Likes

Sorry, I uploaded the code to the original post.

One big error is this, if your using math operators in a run service loop, always use deltaTime.

Position += Velocity * Speed * (60*dt)

Another thing you could do is change the boundaries to exclude that section, so it doesn’t got behind your paddle.