https://i.gyazo.com/466d4c0f30111b3c5a964a4b29b1572d.mp4
This error is WAY too hard to explain in words, so there's the link to a gif of the error above. And this is the code that makes the blocks move:
selectedBlock:MoveTo( Vector3.new( (mouse.Hit.p.X+2) - (mouse.Hit.p.X+2)%4, (mouse.Hit.p.Y+2) - (mouse.Hit.p.Y+2)%4, (mouse.Hit.p.Z+2) - (mouse.Hit.p.Z+2)%4 ) )
Please help!
Hey Chilling_Legacy,
It may help to translate that vector by another vector based on the face of the block you're pointing at. Observe:
-- The size of the blocks. Applied to the offsets in Directions. local BlockSize = 4; -- A list of offsets that help place blocks in the correct orientation given a block face. local Directions = { Enum.NormalId.Right = Vector3.new(0, 0, BlockSize); Enum.NormalId.Top = Vector3.new(0, BlockSize, 0); Enum.NormalId.Back = Vector3.new(-BlockSize, 0, 0); Enum.NormalId.Left = Vector3.new(0, 0, -BlockSize); Enum.NormalId.Bottom = Vector3.new(0, -BlockSize, 0); Enum.NormalId.Front = Vector3.new(BlockSize, 0, 0); }; -- ... -- The position of the target block PLUS a Vector3 representing the correct offset based upon the target face. local relativePos = mouse.Target.Position + Directions[mouse.TargetSurface]; selectedBlock:MoveTo(relativePos);
I hope this helps. I may have assigned the wrong values to the respective Directions, so if the block spawns on the wrong side, that is why. All you need to do to fix that is tweak the Directions table accordingly.
Now, this approach is a little specialized in that it only offsets the current block by BlockSize. It may help to use your original approach as well as my approach alternatively based upon the nature of the current target.
For example, if you tried placing a block on the baseplate, instead of placing at your cursor, the block would be placed at the center of the baseplate, because the block is being positioned at the position of the baseplate plus 0, 4, 0
. Exactly how you rectify this is something you might need to solve yourself.
I hope this helps. Best of luck with your game, Chilling_Legacy. Have a nice day!
All the best,
tkcmdr