Im trying to make a placement system
Objects follow the cursor and will snap to a tile in the grid, I use lerp to smooth the transition between the two
For some reason the light blue tiles spin despite having no orientation given and I have tried applying CFrame.Angles(0, 0, 0) to see if that stops it and it doesn’t
The objects shouldn’t be spinning in the first place
The spinning stops when I remove the :Lerp()
This function is binded to renderstep and is responsible for lerping the objects
local function translateObj()
local coord = Hex.HexPosition(mouse.Hit.X, mouse.Hit.Z, 5.2)
hoveredCoord = coord
if not object then return end
selectionRing:PivotTo(selectionRing.PrimaryPart.CFrame:Lerp(calculatePosition(), speed))
object:PivotTo(object.PrimaryPart.CFrame:Lerp(calculatePosition(), speed))
for i=1, #rangeData, 2 do
local x = rangeData[i]
local z = rangeData[i+1]
local rangeTile = workspace.Visualiser:FindFirstChild(x..z)
rangeTile:PivotTo(rangeTile.PrimaryPart.CFrame:Lerp(calculateRangePosition(x, z), speed))
end
end
from what i understand, your goal is to make it so that the tiles flip when you hover over them once and they’re flipping even while you’re not moving your mouse but you currently have no check for it.
you can add an if statement before setting hoveredCoord to coord and checking if the last coord’s tile is equal to the current tile so that it only happens when you move your mouse over to a new tile
The flipping IS the problem, it shouldn’t be flipping in the first place
So everything else works fine its just the orientation of the object messes up when lerping for some reason
i’m guessing a lot of what the code is since you didn’t show a lot of it but perhaps limiting the speed variable used in lerp so that it doesn’t overshoot the lerp (which shouldn’t happen in the first place) might fix the flipping
local function translateObj()
local function translateObj()
local coord = Hex.HexPosition(mouse.Hit.X, mouse.Hit.Z, 5.2)
hoveredCoord = coord
if not object then return end
+ local clampedSpeed = math.min(speed, 1)
- selectionRing:PivotTo(selectionRing.PrimaryPart.CFrame:Lerp(calculatePosition(), speed))
+ selectionRing:PivotTo(selectionRing.PrimaryPart.CFrame:Lerp(calculatePosition(), clampedSpeed))
- object:PivotTo(object.PrimaryPart.CFrame:Lerp(calculatePosition(), speed))
+ object:PivotTo(object.PrimaryPart.CFrame:Lerp(calculatePosition(), clampedSpeed))
for i=1, #rangeData, 2 do
local x = rangeData[i]
local z = rangeData[i+1]
local rangeTile = workspace.Visualiser:FindFirstChild(x..z)
- rangeTile:PivotTo(rangeTile.PrimaryPart.CFrame:Lerp(calculateRangePosition(x, z), speed))
+ rangeTile:PivotTo(rangeTile.PrimaryPart.CFrame:Lerp(calculateRangePosition(x, z), clampedSpeed))
end
end
if it doesn’t fix it then please provide more code like calculatePosition and calculateRangePosition and how speed changes so we can better find the issue.
snippet:
local function translateObj()
local coord = Hex.HexPosition(mouse.Hit.X, mouse.Hit.Z, 5.2)
hoveredCoord = coord
if not object then return end
selectionRing:PivotTo(selectionRing.PrimaryPart.CFrame:Lerp(calculatePosition(), speed))
object:PivotTo(object.PrimaryPart.CFrame:Lerp(calculatePosition(), speed))
for i=1, #rangeData, 2 do
local x = rangeData[i]
local z = rangeData[i+1]
local rangeTile = workspace.Visualiser:FindFirstChild(x..z)
rangeTile:PivotTo(rangeTile.PrimaryPart.CFrame:Lerp(calculateRangePosition(x, z), speed))
end
end
I used a tutorial to create the base of the placement system and then I changed and added some parts to make it work with the hex grid I am working with if thats any use
This is the section of the code that sets all the variables including speed
function Placement.PreviewPlacement(entity : string, id : number)
if objectId == id then
Placement.CancelPreview(nil, Enum.UserInputState.Begin)
return
end
if object then object:Destroy() end
rangeData = Item("RangeData", Item("TowerData", id).Range).Data
object = Entities[entity].Models[id]:Clone()
objectId = id
renderRange(rangeData)
selectionRing.Ring.Color = invalidPlace
--sets interpolation speed
local preSpeed = 1
if interpolation then
preSpeed = math.clamp(math.abs(tonumber(1-lerpLevel)), 0, 0.9)
speed = 1
end
object.Parent = workspace.Visualiser
selectionRing.Parent = workspace.Visualiser
mouse.TargetFilter = workspace.Visualiser
task.wait()
speed = preSpeed
bindInputs()
end
Functions to calculate where the object should snap to
local function calculatePosition()
local finalCFrame = CFrame.new(0, 0, 0)
local x, z
posY = mouse.Target.Position.Y + mouse.Target.Size.Y/2
if snapToGrid then
local worldPos = Hex.WorldPosition(hoveredCoord, 5.2)
finalCFrame = CFrame.new(worldPos.X, posY, worldPos.Z)
else
finalCFrame = CFrame.new(mouse.Hit.X, posY, mouse.Hit.Z)
end
finalCFrame = finalCFrame * CFrame.Angles(0, math.rad(rotation), 0)
return finalCFrame
end
local function calculateRangePosition(x, z)
local offsetPos = Hex.Add({x, z}, hoveredCoord)
local pos = Hex.WorldPosition(offsetPos, 5.2)
return CFrame.new(Vector3.new(pos.X, posY, pos.Z)) * CFrame.Angles(0, math.rad(90), 0)
end
local function calculatePosition()
local finalCFrame = CFrame.new(0, 0, 0)
local x, z
posY = mouse.Target.Position.Y + mouse.Target.Size.Y/2
if snapToGrid then
local worldPos = Hex.WorldPosition(hoveredCoord, 5.2)
finalCFrame = CFrame.new(worldPos.X, posY, worldPos.Z)
else
finalCFrame = CFrame.new(mouse.Hit.X, posY, mouse.Hit.Z)
end
finalCFrame = finalCFrame * CFrame.Angles(0, math.rad(rotation), 0)
return finalCFrame
end
is there a reason you have this rotation here? i don’t see what the variable looks like anywhere in your code, can you test it without it and see if that fixes it?
That rotation is so that the object’s cframe updates with how many times the user wants to rotate, the code for rotation is just
rotation += rotationAngle
With rotationAngle being 60
I don’t see why cacluclatePosition() would be part of the problem when the two objects that is being applied to (the red ring and the player model) doesn’t have any issues themselves
While calculateRangePosition() is the one making the cframe for the light blue tiles
Also I did remove it to see what it would do and it just stopped me from being able to rotate the object and didn’t fix the problem
I’ve found a solution so,
I’ll put here how I fixed it if anybody has the same issue:
Make sure that the object is inside of a model
Create a part that is Anchored and CanCollide is false
Put the part in the centre of the object, via copy and pasting the position from the object to the part
Set the model’s PrimaryPart as the part that you’ve just created
You can set the part’s Transparency to 1 if you don’t want it to be seen
I still don’t know why this works