How to make spawned house not clip into baseplate? (Position from mouse)

Hey guys,

In this script I am trying to spawn a House, or building onto my mouse position, using the :GetMouse() function. This is basically replicated tower defense or city maker games, just on a super basic level.

The issue im finding with this script is that the model spawns halfway through the baseplate, instead of its bottom being connected to the baseplate.

example of this: Gyazo link of problem

Is there any setting in the model that I could activate so that it cant go through, or is it something to do with scripting it in. any help is appreciated

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnFolder = ReplicatedStorage.Spawnables

local Switch = false
local debounce = false
local OnexButton = script.Parent.MiniHouse
local OnexTButton = script.Parent.LongHouse
local TwoxOButton = script.Parent.ScrapperHouse

--detection 
OnexButton.MouseButton1Down:Connect(function()
	if Switch == false then
		Switch = true
		
		print("Started placing 1x1")
		UIS.InputBegan:Connect(function(key, processed)
			if processed then
				return
			end

			if key.UserInputType == Enum.UserInputType.MouseButton1 then -- Detects if mouse is clicked
				if Switch == true then
					if debounce == false then
						debounce = true
						local SpawnableHouse = ReplicatedStorage.Spawnables.MiniSquare:Clone()
						SpawnableHouse.PrimaryPart.Position = game.Players.LocalPlayer:GetMouse().Hit.p
						SpawnableHouse.Parent = workspace
						print("Right mouse button clicked! (1x1)")

						debounce = false
						Switch = false
				end
				end
			end
		end)
		
		
	end

	
end)
OnexTButton.MouseButton1Down:Connect(function()
	if Switch == false then
		Switch = true
		
		print("Started placing 2x1")
		UIS.InputBegan:Connect(function(key, processed)
			if processed then
				return
			end

			if key.UserInputType == Enum.UserInputType.MouseButton1 then -- Detects if mouse is clicked
				if Switch == true then
					if debounce == false then
						debounce = true
						local SpawnableHouse = ReplicatedStorage.Spawnables.MiniLong:Clone()
						SpawnableHouse.PrimaryPart.Position = game.Players.LocalPlayer:GetMouse().Hit.p
						SpawnableHouse.Parent = workspace
						print("Right mouse button clicked! (2x1)")

						debounce = false
						Switch = false
					end
				end
			end
		end)
		
		
	end

end)
TwoxOButton.MouseButton1Down:Connect(function()
	if Switch == false then
		Switch = true
		
		print("Started placing 1x2")
		UIS.InputBegan:Connect(function(key, processed)
			if processed then
				return
			end

			if key.UserInputType == Enum.UserInputType.MouseButton1 then -- Detects if mouse is clicked
				if Switch == true then
					if debounce == false then
						debounce = true
						local SpawnableHouse = ReplicatedStorage.Spawnables.MiniTall:Clone()
						SpawnableHouse.PrimaryPart.Position = game.Players.LocalPlayer:GetMouse().Hit.p
						SpawnableHouse.Parent = workspace
						print("Right mouse button clicked! (1x2)")

						debounce = false
						Switch = false
					end
				end
			end
		end)
	end

end)

 

any help is appreciated, thank you

3 Likes

You have to account for the side of the model you are place so it would be the mouse position + the Y / 2 + .5 of the extents size of the model your place

Vector3.new(0, extents.Y / 2 + 0.5, 0)
2 Likes

Hey, I tried what you said but it didnt work.

I ended up changing it around a bit, and even testing the other axis’ but neither worked.

This is what I came up with in the end, still doesnt change it, Neither .Position or .CFrame change what happens when spawned.

Vector3.new(0, SpawnableHouse.PrimaryPart.CFrame.Y / 2 + 10, 0)

Full script snippet:

	debounce = true
	local SpawnableHouse = ReplicatedStorage.Spawnables.MiniSquare:Clone()
	SpawnableHouse.PrimaryPart.Position = game.Players.LocalPlayer:GetMouse().Hit.p
	Vector3.new(0, SpawnableHouse.PrimaryPart.CFrame.Y / 2 + 10, 0)

	SpawnableHouse.Parent = workspace
	print("Right mouse button clicked! (1x1)")

    debounce = false
    Switch = false
1 Like

Well it should be half the size of the Y not it’s position could you show your code before with using the extentsSize of the model please?

2 Likes

Sorry!

I completely misunderstood what you said in the first answer, but I then realized my mistake. Thank you for your help, after I edited what you sent by a little, I have managed to make it work haha.

Completed task!!!

Just for future reference if anyone ever sees this,

local SpawnableHouse = ReplicatedStorage.Spawnables.MiniTall:Clone()
local MousePos = game.Players.LocalPlayer:GetMouse().Hit.p
SpawnableHouse.PrimaryPart.Position =  Vector3.new(MousePos.X,MousePos.Y + SpawnableHouse.PrimaryPart.ExtentsSize.Y / 2, MousePos.Z)

That is the correct math behind the spawning system

2 Likes

actually

mouse.Button1Down:Connect(function()
	local MousePos = mouse.Hit.Position
	SpawnableHouse:PivotTo(CFrame.new(Vector3.new(MousePos.X,MousePos.Y+(SpawnableHouse.PrimaryPart.Size.Y/2),MousePos.Z)))
end)

would be better …

2 Likes

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