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

How do you move a model using the TweenService?

Asked by
Nowaha 459 Moderation Voter
4 years ago
Edited 4 years ago

Thanks for checking out my question!

I was wondering how I can move a model smoothly when using ":SetPrimaryPartCFrame()" on the model.

I'd like to use the TweenService to achieve this effect. Is there maybe a better way to move it than SetPrimaryPartCFrame(), like a value, so I can use the TweenService?

On a side note, can I move it without a primary part? I don't really want to put a middle part inside all the models I'll be using. I know model:MoveTo() exists but this causes it to always be on top, which isn't what I want because it also glitches out when it's at your player.

This is my code:

wait(2);

local previewItem = game.ReplicatedStorage["Wood Crate"]:Clone();
previewItem.Parent = workspace;

local exclusions = {};

for index, descendant in pairs(previewItem:GetDescendants()) do
    if descendant.ClassName == "Part" then
        descendant.Transparency = 0.5;
        descendant.CanCollide = false;
        descendant.Anchored = true;
        table.insert(exclusions, descendant);
    end;
end;

table.foreach(game.Players.LocalPlayer.Character:GetDescendants(), function(i, obj)
    if obj.ClassName == "Part" or obj.ClassName == "MeshPart" or obj.ClassName == "Hat" then
        table.insert(exclusions, obj);
    end
end);

local mouse = game.Players.LocalPlayer:GetMouse();
local camera = game.Workspace.CurrentCamera;

function round(n)
    return math.floor(n + .5)
end

mouse.Move:Connect(function()
    local unitRay = camera:ScreenPointToRay(mouse.X, mouse.Y);
    local ray = Ray.new(unitRay.Origin, unitRay.Direction * 500);

    local part, pos = workspace:FindPartOnRayWithIgnoreList(ray, exclusions);

    if part then
        previewItem:SetPrimaryPartCFrame(CFrame.new(Vector3.new(round(pos.X), round(pos.Y + (previewItem:GetExtentsSize().Y / 2)), round(pos.Z))));
    end;
end);

If any part of the code can be more efficient, like the exclusions, please let me know too :)

tldr; I want a model to follow the mouse on a grid with raycasting, smoothly, and preferrably without setting the PrimaryPart. How?

0
u dont. set primary part, anchor it, unanchor every other part, weld it to primary part, tween. Elixcore 1337 — 4y

1 answer

Log in to vote
0
Answered by
U_srname 152
4 years ago

Here is a way I found out recently. First, I'll give you the code then explain:

-- Replace line 37 with this

previewItem:TranslateBy(Vector3.new(round(pos.X), round(pos.Y + (previewItem:GetExtentsSize().Y / 2)), round(pos.Z)))

First, you might need to change your math since you aren't using SetPrimaryPartCFrame, I haven't checked that.

So, recently I was looked at the Scripting Helpers Tip of the Day and found this. I did more research and found I was actually looking for this a few weeks ago.

As explained by the Roblox Developer site:

Shifts a Model by the given Vector3 offset, preserving the Model's orientation. If another BasePart or Terrain already exists at the new position then the Model will overlap said object.

The only issue with this might be that it translates in world space.

Hope I helped!

0
It isn't possible using tween service, but this should give the same effect U_srname 152 — 4y
0
TranslateBy shifts by an offset, but I'm trying to move it to a specific location. Nowaha 459 — 4y
Ad

Answer this question