✓ Accepted answer
Since you said your welding your part to the player via the root part of the humanoid you can't use the velocity like for instance if the player was stationary the Vector3.unit would be dividing by zero so you're positioning Vector will return NAN or not a number. to get around this problem you could record the position of your part each tick then save the previous position to get the direction from subtracting one from the other and if we're not moving use the lookvector of the root part as reference.
Something like:
local root = script.Parent
local Pos = root.Position
local OldPos = Pos
local DBug_Part = (function()
local P = Instance.new("Part",workspace)
P.Anchored = true
P.Size = Vector3.new(1,1,1)
return P
end)()
while true do
Pos = root.Position
local DeltaPos = (Pos - OldPos)
direction = DeltaPos.magnitude ~= 0 and DeltaPos.unit or root.CFrame.LookVector
DBug_Part.Position = Pos + (direction *2)
print(direction)
OldPos = Pos
wait(0.2)
end
a Much nicer way to make sure your part 'Remembers' which way it was pointing is to change the above code to the following:
local root = script.Parent
local Pos = root.Position
local OldPos = Pos
local OldDir = Vector3.new()
local DBug_Part = (function()
local P = Instance.new("Part",workspace)
P.Anchored = true
P.Size = Vector3.new(1,1,1)
P.CanCollide = false
return P
end)()
while true do
Pos = root.Position
local DeltaPos = (Pos - OldPos)
direction = DeltaPos.magnitude ~= 0 and DeltaPos.unit or OldDir
DBug_Part.Position = Pos + (direction *2)
OldPos = Pos
OldDir = direction
wait()
end
Kinda related to this but I've made a little example from the above code that moves a part in a sine wave in 3 directions and positions a part in the direction the part is travelling.
Both parts are anchored!
Code: (your free to use this code however you want. I just threw it together lol
local root = script.Parent
local Pos = root.Position
local OldPos = Pos
local OldDir = Vector3.new()
local OffsetPos = root.Position
local DBug_Part = (function()
local P = Instance.new("Part",workspace)
P.Anchored = true
P.Size = Vector3.new(1,1,1)
P.CanCollide = false
return P
end)()
local tx = 0
local ty = 0
local tz = 0
root.FrontSurface = Enum.SurfaceType.Hinge
while true do
local ax = tx * math.pi/180
local ay = ty * math.pi/90
local az = tz * math.pi/45
local X = 10 * math.cos(ax)
local Y = 10 * math.sin(ay)
local Z = 10 * math.sin(az)
root.CFrame = CFrame.new(OffsetPos+Vector3.new(X,Y,Z), Pos)*CFrame.Angles(0,math.rad(180),0)
Pos = root.Position
local DeltaPos = (Pos - OldPos)
direction = DeltaPos.magnitude ~= 0 and DeltaPos.unit or OldDir
DBug_Part.Position = Pos + (direction *3)
OldPos = Pos
OldDir = direction
wait()
tx = tx+0.5
ty = ty+0.7
tz = tz+0.2
end
2 comments
That's great. I'm still going over it to make sure I understand it fully. Thank you for taking the time to explain it in such depth.
your welcome mate! :)