Hi folks,
I have a project in which you’d be able to move parts around using DragDetectors. The area you can build in is limited with visual parts & with a Vector3 clamper.
Chapters:
My issue
The `DragDetector:AddConstraintFunction()` method is quite oddly working to me. I may be doing wrong things (and that is probably what is happening), but the constraint I am looking for is **NOT** working.The idea
The constraint is made so that, if you go out of bounds, the part cannot go past it: 1. Minimum coordinates: `x = -2048, y = 0, z = -2048` 2. Maximum coordinates: `x = 2048, y = 2048, z = 2048`There is also a more physical approach. When your part collides with the border (materialized by the red grid ou can see here:)
If the part touches the border, it stops. This is collision build and this feature is also part of Studio.
Position clamping
I solve the position clamping with a function calledReplicatedFunctions:ClampVector3(V: Vector3, Min: Vector3, Max: Vector3): Vector3
This function allows me to clamp the Position Vector3
accordingly.
Collision building
I simply put the part in where it *should* be, I then run a function calledReplicatedFunctions:ReturnBoundingParts(List: {Instance}, Strict: boolean?): {BasePart}
where List
is the list of parts to analyze (aka check if foreign parts are touching it), Strict
tells the script if only red borders should be analyzed (Strict = false
which is the default), or if it should make a pure collision building as in Studio (check every foreign parts from the selection, Strict = true
)
This function returns a list of colliding parts, which can describe if one or more foreign parts are touching the selection. (#ReplicatedFunctions:ReturnBoundingParts(List, Strict) > 0
)
The issue
Expected behavior
As its arrow handle brother (shown in the video below), when the part **touches** the red border, it does NOT go any further.Current behavior
The constraint isn't working properly. Here is the script with functions I detailed earlier. All the selection is in a model called SelectionModel
. The DragDetector is parented to this model. The HandleSelectionPart
is a part that is the adornee of some Handles to show axes. This part is moved first, then the whole SelectionModel
is moved using
ReplicatedFunctions:ApplyPosition(Model: Model, Position: Vector3): ()
--PVInstanceObject is a table with a key "Instance" pointing to the Instance it is associated with. OOP scripting.
local startCFame: CFrame --CFrame initialized with DragStart
function DragConstraint(Inst: TypesModule.PVInstanceObject, ProposedMotion: CFrame): CFrame
if isMoving then
if Inst.Instance:IsA("Model") then
SelectionModel.PrimaryPart = Inst.Instance.PrimaryPart
else
SelectionModel.PrimaryPart = Inst.Instance
end
--Cache the current (aka "old") CFrame
local cachedCFrame = Inst.Instance:GetPivot()
--Moves the handle at the desired position
HandleSelectionPart.Position = startCFrame.Position + ProposedMotion.Position
--Clamps the position
HandleSelectionPart.Position = ReplicatedFunctions:ClampVector3(HandleSelectionPart.Position, Vector3.new(-2048, 0, -2048), Vector3.new(2048, 2048, 2048) )
--Moves the SelectionModel to where the handle is
ReplicatedFunctions:ApplyPosition(SelectionModel, HandleSelectionPart.Position)
--Check if the instance is not interfering with borders, ...: if it does, use cachedCFrame
if #ReplicatedFunctions:ReturnBoundingParts(SelectionModel:GetChildren(), false) > 0 then
print("Part touching border. Reverting to previous position.")
--Places the Handle & the model back to the previous position
HandleSelectionPart.Position = cachedCFrame.Position
ReplicatedFunctions:ApplyPosition(SelectionModel, cachedCFrame.Position)
--Transmits the previous position to the DragDetector
return ProposedMotion.Rotation + (cachedCFrame.Position - startCFrame.Position)
else
--Gives the ProposedMotion as it is valid
return ProposedMotion
end
else
--In case the system is unset, returns 0
return CFrame.new(0, 0, 0) * CFrame.Angles(0, 0, 0)
end
end
This looks right to me (although I struggled to understand how it works), but here’s the result:
Conclusion
I may have not understood how DragDetectors were made. Due to my French nationality, it is sometimes hard for me to understand documentation.
If you think my problem is untreatable via AddConstraintFunction
, I believe it is as I have seriously checked different topics about that, and posts that @PrinceTybalt made in the two following posts:
I even tested the testplace, but I could not find my use case. Although I found applications where restrictions were applied to dragging.
Thanks for having read the post, and having helped me,
Varonex_0