Cars are not moving correctly

I was starting to make good progress on my new game but I couldn’t really get how why this isn’t working.


Here is the code,

function moveCar(Part:BasePart,Goal:Vector3)
	local Hit = MS.Hit.Position
	
	local Delta = Vector3.new(Hit.X,0,Hit.Z) - Vector3.new(lastPos.X,0,lastPos.Z)
	
	local Offset = Part.CFrame.LookVector * Vector3.new(Delta.X,0,Delta.Z)
	
	Part.Position += Offset
end
if DraggedPart then
	MS.TargetFilter = Level
		
	moveCar(DraggedPart,MS.Hit.Position)

	MS.TargetFilter = nil
end

Anyone know why it isn’t working? Thanks.

What’s not working about it? Is it supposed to collide?

You can check the width and length (Height is irrelevant I think), and then check if the other part’s width and length, and use this formable:

function isColliding(rect1:Vector3, rect2:Vector3)
    return (rect1.Position.x < rect2.Position.x + rect2.Size.x &&
   rect1.Position.x + rect1.Size.z > rect2.Position.x &&
   rect1.Position.y < rect2.Position.y + rect2.Size.z &&
   rect1.Position.y + rect1.Position.y > rect2.Position.x)
end

(Note: I got this from a js code, and edited it, so it might have some syntax issues, also the Position and Size and x and y values might be mixed up.)

Edit: I just wanted to add, that if we use a variable that is a bool (I’ll call it CanUpdate), and a variable that is a Vector3 (I’ll call it PreviousPos). Then we can edit your code:

local CanUpdate = true
--local PreviousPos = -- get the position of the car

function moveCar(Part:BasePart, Goal:Vector3)
	if canUpdate then
		canUpdate = false
		
		local colliding = false
		
		-- loop though the hitboxes and put them into the function, replace "table" with this list
		in i, hitBox in ipairs(table) do
			colliding = isColliding(Goal, hitBox)
			if colliding then
				break
			end
		end

		if isColliding then
			local Hit = MS.Hit.Position
	
			local Delta = Vector3.new(Hit.X,0,Hit.Z) - Vector3.new(lastPos.X,0,lastPos.Z)
	
			local Offset = Part.CFrame.LookVector * Vector3.new(Delta.X,0,Delta.Z)
	
			Part.Position += Offset
		end
	end
end

(Wait I just realized PreviousPos is redundant lol.)

1 Like

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