So this is what I am trying to achieve.
A model with a primarypart should find a part under it that is closest to it and then just stick on to the part.
So this is what I am trying to achieve.
A model with a primarypart should find a part under it that is closest to it and then just stick on to the part.
ray cast from the center of the model. when the ray hits a part, you can make the model go down by the length of your ray which you can calculate using magnitude. (StartPos-EndPos).Magnitude this will give you a distance in studs which you can move your part by.
local Workspace = workspace --Integrate into local environment.
local Model = Workspace.Model --Examples.
local ModelSize = Model:GetExtentsSize()
local _RaycastResult = Workspace:Raycast(Model.PrimaryPart.Position, Model.PrimaryPart.CFrame.UpVector * -100) --Raycast from primary part's position to 100 studs below.
if _RaycastResult then --Check if ray intercepted something.
Model:PivotTo(_RaycastResult.Instance.CFrame * CFrame.new(0, _RaycastResult.Instance.Size.Y / 2, 0) * CFrame.new(0, ModelSize.Y / 2, 0)) --Move model to intercepted part's position while accounting for the part's size and the model's size.
end
Before:
After:
This can also be tweened (if necessary).
local Game = game --Local environment.
local Workspace = Game:GetService("Workspace")
local Tweens = Game:GetService("TweenService")
local Model = Workspace.Model --Examples.
local ModelSize = Model:GetExtentsSize()
task.wait(3)
local _RaycastResult = Workspace:Raycast(Model.PrimaryPart.Position, Model.PrimaryPart.CFrame.UpVector * -100) --Raycast from primary part's position to 100 studs below.
if _RaycastResult then --Check if ray intercepted something.
local Tween = Tweens:Create(Model.PrimaryPart, TweenInfo.new(3), {CFrame = _RaycastResult.Instance.CFrame * CFrame.new(0, _RaycastResult.Instance.Size.Y / 2, 0) * CFrame.new(0, ModelSize.Y / 2, 0)})
Tween:Play()
end
why did you use Game instead of game and Workspace instead of workspace?
Also I have an issue the other parts in the model get hit by the raycast…
Hi!
Personally I find no reason for this. use “workspace” over game.workspace/game:GetService(“Workspace”), and don’t make a variable tha defines “game”.
Then use a RaycastParams object so you can blacklist the model’s descendants.
https://developer.roblox.com/en-us/api-reference/datatype/RaycastParams
local Workspace = workspace
local _RaycastParams = RaycastParams.new()
_RaycastParams.FilterDescendantsInstances = {Model} --Table of instances to filter.
_RaycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local _RaycastResult = Workspace:Raycast(Origin, Direction, _RaycastParams) --Origin and direction represent your ray's origin and its direction.
You can find more information regarding local environment integration here.