Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Getting the reverse angle of Mouse.TargetSurface help?

This is a pretty hard idea to explain so bare with me

Alright, so what I'm trying to do is get the Mouse.TargetSurface right then based of what angle that is place a block reflected away from it but at the same position of the block we're targeting.

Here's some visuals: http://prntscr.com/9m76jh, http://prntscr.com/9m7757


Helping you understand:


Alright let me try using and example to help you better understand. Let's say Billy wants to put one brick next to one another.

Firstly, he would select his part then put his mouse on the side of the currently placed block (VISUAL: http://prntscr.com/9m7b4a).

Secondly, he would then see his ghost item / placing item appear like so (VISUAL:http://prntscr.com/9m7ci4).

Thirdly, he would then click and place the item with this being his final result and ending product (VISUAL: http://prntscr.com/9m7ctn).


The script:


Before you write an annoying answer about how there is an 'error' in the script keep in mind there is no error, I'm just having problems with the math. This script works perfectly fine as is but I need to better it.

(Recommend viewing source so you can see the full script's width)

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Camera = game.Workspace.CurrentCamera
local Database	= game.ReplicatedStorage
local Mouse  = Player:GetMouse()

function updatePlacing()
	if currentlyPlacing == true then
		if previous ~= nil then previous:Destroy() end
		if itemSetName ~= nil then
			item = itemSetName:Clone()
			previous = item
			for i,v in pairs (item:GetChildren()) do
				if v:IsA("Part") then
					v.Transparency = .7
					v.BrickColor   = BrickColor.Green()
					v.CanCollide   = false
				end
			end
			item.Parent = Character
			if item.Settings.Tiltable.Value then
				item:SetPrimaryPartCFrame(item.PrimaryPart.CFrame*CFrame.Angles(currentTilt,0,0))
			end
			if item.Settings.Rotateable.Value then
				item:SetPrimaryPartCFrame(item.PrimaryPart.CFrame*CFrame.Angles(0,currentRotation,0))
			end
			local t = {}	
			for i,v in pairs (snappingList) do
				if Mouse.Target == v then
					item:SetPrimaryPartCFrame(CFrame.new(v.Position)*CFrame.Angles()) --Set the CFrame [Problem line]
					break
				else
					item:SetPrimaryPartCFrame(CFrame.new(Mouse.hit.p)) --If not snapping then let the brick go where the mouse goes
				end
			end	
				
		end
	end		
end

1 answer

I'm not 100% sure what is going on with some of the stuff in your script, however there is an easy way to accomplish what you want.

All you have to do is set the CFrame of the thing you are trying to snap to that of the thing you are snapping onto, and multiply by size of the thing you are trying to snap with the vector of the NormalID.

local extents = item:GetExtentsSize() -- Or PrimaryPart Size, etc. (Vector3)
local cframe = CFrame.new(extents*Vector3.FromNormalId(Mouse.TargetSurface))
item:SetPrimaryPartCFrame(v.CFrame*cframe)

2 comments

0

(It's actually GetExtentsSize but thanks I completely forgot that I could do that) Now to work on the angling: https://gyazo.com/e08e5ec3d54cd382a0a4d9c5cdb722da 

0

Whoops my bad, glad I could help!

This discussion is preserved as it was. New answers, comments, and votes are closed.