Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[SOLVED] How can I allow :MoveTo to go to exact coordinates even when moving inside of objects?

Asked by 4 years ago
Edited 4 years ago

My problem is that models teleport upwards when they're inside of objects, even if their parts have CanCollide set to false and Anchored set to true. The script is this:

local x = 0
local c = 0
--all this script is doing is oscillating the X position back and forth
while(wait(1/60)) do
    c = c + math.pi/60
    x = math.sin(c)*10
    script.Parent:MoveTo(Vector3.new(x,1,1))
end

The object is this:
https://i.imgur.com/Yem7zXY.png
https://i.imgur.com/bi9Ol9f.png
The expected outcome is this:
https://i.imgur.com/m6TpJmv.mp4
What I actually get when using a model is this:
https://i.imgur.com/ayjcB9k.mp4

How can I force the model's position to be that of any Vector3, even if it's inside of another object?

Here's a link to download this .rbxl featured in the above screenshots

2 answers

Log in to vote
1
Answered by 4 years ago

The issue you're experiencing is that a :MoveTo() operation will avoid any obstructions.

Here's a quote from the Developer Hub:

If there are any obstructions where the model is to be moved to, such as Terrain or other BaseParts, then the model will be moved up in the Y direction until there is nothing in the way. If this behavior is not desired, Model:SetPrimaryPartCFrame should be used instead.

(https://developer.roblox.com/en-us/api-reference/function/Model/MoveTo)

For this type of movement, I'd recommend welding (Welds not WeldConstraints) each part of your model to an Anchored PrimaryPart, and unanchoring all the welded parts. With this, you can simply use a :SetPrimaryPartCFrame operation to move the model's PrimaryPart, and the rest should follow.

Hope this helps!

1
Thanks, this is the kind of answer I was looking for! ArtsicleOfficial 171 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

I pretty much created my own MoveTo function to fix this. I realized that normal parts don't suffer from this issue, only models, so I moved all parts by moving them to the new position and then adding the distance from the center of the object to their new spot to keep the children at their relative positions.

function MoveTo(model,position)
    local Center = model:GetBoundingBox().Position
    for i,v in pairs(model:GetChildren()) do
        if(v.ClassName == "Part" or v.ClassName == "UnionOperation") then
            local Difference = v.Position - Center
            v.Position = position + Difference
        end
    end
end

Answer this question