How can I use BodyVelocity to get a Part to move to a player?

My issue isn’t that complicated now that I think of it. I’m trying to script a part so it’ll move to a player(or a model to be less specific). I tried using CFrame.lookAt().p and CFrame.new(pos1,pos2).p and both did not work. How can I achieve my goal? All answers appreciated.

1 Like

You might just be getting confused on the vector math involved. Subtract the start vector (current location of the subject) from the end vector (your destination) to get the displacement vector. Normalize that to get a unit vector (length 1), then multiply that by your desired velocity.

local RunService = game:GetService("RunService")
local part = script.Parent
local bv = part.BodyVelocity

function getVelocity()
    -- Extra credit: turn this value into an Attribute and use GetAttribute :)
    return 12
end

function getDestination()
    -- Up to you to determine where this thing is going!
    return workspace.SomePlayer.HumanoidRootPart.Position
end

function update()
    local offset = getDestination() - part.Position
    bv.Velocity = offset.Unit * self:getVelocity()
end
update()
RunService.Stepped:Connect(update)

Note: I’m using RunService.Stepped here because the target could be in motion and thus we’ll need to update the velocity repeatedly. For unmoving destinations, you won’t need to connect this up. Avoid the temptation to use while wait() do update() end here, as this will not scale well if you plan on having a lot of these (eg. homing projectiles).

2 Likes

Better yet, you could use a RocketPropulsion object, change its properties, then activate its Fire() function to move towards the player

local Part = script.Parent
local Rocket = Part.RocketPropulsion

--Properties To Change--
Rocket.Target = workspace.RandomPlayer.HumanoidRootPart

--Fire the Object using the Fire() function
Rocket:Fire()
1 Like

Apologies for the late response, i’ll be trying this right now.

I will also attempt to implement your solution in a few moments, i’ll mark the solution which works the best.

Actually, I don’t think the script translates well when you try to use it on a model.

Wasn’t able to get it to work after playing around with it. It worked fine with a part but when I put it inside a model it never moved.

If you referenced the RocketPropulsion’s Target right, it needs to be a BasePart & not the Model object itself

If it still didn’t work, can I see the script you’re using?

local rock = Instance.new("RocketPropulsion")
    rock.Parent = rot --HumanoidRootPart of model
	rock.Target = workspace.Part
	rock:Fire()

This is the code I used.

Are you sure you didn’t mix the 2 up…? The rock’s Parent would be Part that it would be moving to, from this point it looks like you’re wanting the RootPart to move to the part?

    local rock = Instance.new("RocketPropulsion")
    rock.Parent = workspace.Part
	rock.Target = rot
	rock:Fire()

Yes, I also want the HRP to move to the part, but it doesn’t work whenever I try to do that.

Then you could just use the Humanoid:Move() function? The only issue is that you’d need to disable the Player Controls, otherwise the RenderStepped event inside the Control Module

Humanoid:Move(workspace.Part.Position)

No need for a BodyVelocity object if you’re wanting to move the player to a part

The model doesn’t have a Humanoid inside so I can’t do that(it’s a stand model).

Ok use the Player:Move() function then on a LocalScript

If the Character Model you’re using doesn’t have a Humanoid Object then you could still try that

Otherwise there isn’t much options we could think of here

I resolved the issue on my own (MeshPart caused RocketPropulsion to act up).