Calculations for CFrame are inaccurate from what I want it to be

I’m trying to make a tool that drops piano’s from the sky, and with that tool comes a part named “pianoMark” that visually marks down where the piano will fall, an issue with it that I’ve been trying to solve for a while now though is that the piano mark just kind of floats mid air, and it doesn’t stay on the ground. I’ve attempted once again to come up with a solution by firing down a raycast on another part named “initialPosition” which is where we will get our initial CFrame from, and then after that I try making the pianoMark’s CFrame relative to the initialPosition’s CFrame, while also getting the Y axis of the raycast so that I can correctly make it stay on the ground. But this hasn’t work, and I’ve received a more broken looking effect.

What should I do?

function pianoMarkPosition()
	local initialPosition = Tool:FindFirstChild("initialPosition") -- A part that I'm trying to use to raycast downwards and have its cframe  be in front of the player
	initialPosition.Anchored = true
	initialPosition.CFrame = Player.Character.PrimaryPart.CFrame * CFrame.new(0, 0, basePianoMarkDistance + -pianoMarkMultiplier)
	
	raycastResult = workspace:Raycast(initialPosition.Position, Vector3.new(0, -1000, 0))
	
	if raycastResult then
		if raycastResult.Instance:IsA("Part") then
			pianoMark:PivotTo(initialPosition.CFrame * CFrame.new(0, raycastResult.Position.Y, 0)) -- formula for the position of the pianoMark
		end
	end
end
1 Like

Could you provide the tool or a screenshot of what it’s doing and what you’re intending it to look like? I notice the InitialPosition is a part inside of the tool, is there a reason you do that instead of creating a part that can be cleaned up with Debris? :slight_smile:

1 Like

Hello,

I don’t have any specific reasoning behind it, but will your recommendation help me with optimization?

p.s, video’s laggy because I was trying to compress it and also the game was a bit laggy because of a script I still have yet to disable.

It’s not going to make that much a change in performance, but personally I would create the mark when tool is equipped and destroy it when unequipped.

What are the variables “basePianoaMarkDistance” and “pianoMarkMultiplier”?

That video definitely helped, initially I was thinking you were wanting something completely different. Thanks!

1 Like

temporary add a raycastparams to filter out initialPosition part from being hit if needed

i think you wanted to do this

pianoMark:PivotTo(initialPosition.CFrame * CFrame.new(0, raycastResult.Position.Y - initialPosition.Position.Y , 0))

or

pianoMark:PivotTo(CFrame.new(raycastResult.Position) * initialPosition.CFrame.Rotation)

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