-- vec3 being the target to reach
-- obj being the Instance
-- self.speed being 10
local v = (vec3 - obj.Position) *Vector3.new(1, 0, 1)
local timeNeeded = v.Magnitude/self.speed
obj.BodyVelocity.Velocity = v.Unit * self.speed
wait(timeNeeded)
obj.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
-- finished
https://gyazo.com/85b9418cee74d7901f7ec64d44df1faa
^ If I have the BodyVelocity’s velocity to be simply just the unit vector and not multiply it.
It maintains its direction but of course at a slower speed.
Solved.
Issue was that the part was dragging along the floor.
There is nothing incorrect about your vector calculations (although I’m not sure it would reach the target every time).
I’ve just created a reproduction in Studio and experienced no issue at all. Let me know if you see any differences. I placed a BodyVelocity inside of Part with all default values.
local v = (workspace.Goal.Position - workspace.Part.Position)*Vector3.new(1, 0, 1)
local timeNeeded = v.Magnitude/10
workspace.Part.BodyVelocity.Velocity = v.Unit * 10
wait(timeNeeded)
workspace.Part.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
This repro goes in the correct direction every single time, so I think the problem exists outside of your math but I am not sure where.
One alternative (and this is what I do for my classed AI units), is to use CFrame a little bit instead like so (and you can obviously switch to BodyVelocity):
local Goal = workspace.Goal
local Part = workspace.Part
local ImpulseStrength = 15
Part.CFrame = CFrame.new(Part.Position, Goal.Position)
while (Goal.Position - Part.Position).magnitude > 5 do
Part:ApplyImpulse(Part.CFrame.LookVector * ImpulseStrength) wait()
end
Part.AssemblyLinearVelocity = Vector3.new(0,0,0)
Thanks for the reply!
Theoretically it should have worked, hence I was confused.
But the issue was that the part was dragging along the ground(?)
Set the Y value to 5 to get it off the ground and indeed it maintained its direction.
Regarding CFrames, they are the equivalent to what I was doing with the vectors. But it was still appreciated! <3