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

[Unsolved] How do I calculate a Vector3 position with a given distance and direction?

Asked by 9 years ago

So I have a script that, when I click, a part moves from it's position to the mouse.Hit.p position, but I also want it to be limited to a maximum distance, how would I recalculate the target position to a new point with the maximum distance in the same direction.

I recreated my situation below, copy and paste it in a new project. Put it in a LocalScript in StarterPack.

local m = game.Players.LocalPlayer:GetMouse()
local p = Instance.new("Part", workspace)
local v = Instance.new("BodyVelocity", p)
local c
local welded = false
p.Anchored = true
local point
wait()

function getDir(vec)
    local lenSquared = vec.magnitude^2
    local invSqrt = 1 / math.sqrt(lenSquared)
    return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end

function ShowPointer(target)
    local point = Instance.new("Part", game.Workspace)
    point.Name = "Point"
    point.Size = Vector3.new(1,1,1)
    point.CanCollide = false
    point.Anchored = true
    point.Position = target + Vector3.new(0,1,0)
    point.BrickColor = BrickColor.new(21)
    local mesh = Instance.new("BlockMesh", point)
    mesh.Scale = Vector3.new(0.3,1,0.3)
    return point
end

function Unweld()
    if p:FindFirstChild("Weld") then
        p.Weld:Destroy()
    end
    welded = false
end

function Weld(char)
    p.Anchored = false
    c = char
    local torso = c.Torso
    Unweld()
    local weld = Instance.new("Weld")
     weld.Part0 = p
     weld.Part1 = torso
     weld.Parent = p
     weld.C0 = CFrame.new(0,2,3)
     weld.C1 = CFrame.new()
    welded = true   
end

function Move(target, maxDist)
    if c ~= game.Players.LocalPlayer.Character then return end
    p.Anchored = false
    Unweld()
    target = Vector3.new(target.X, p.Position.Y, target.Z)
    local dist = (target - p.Position).magnitude
    local dir = (target - p.Position).unit
    dist = math.min(dist,maxDist)
    --target = dir*dist -- If I uncomment this line, it stops working properly.
    if point ~= nil then point:Destroy() end
    point = ShowPointer(target)
    p.BodyVelocity.velocity = getDir(dir) * 30.0
    while dist > 0.5 and wait() do dist = (target - p.Position).magnitude end
    p.BodyVelocity.velocity = Vector3.new(0,-10,0)
    p.Anchored = true
end

function OnButton2Up()
    if not welded then return end
    Move(m.Hit.p, 30)
end

m.Button2Up:connect(OnButton2Up)
p.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        Weld(hit.Parent)
    end
end)

If I uncomment the line I mentioned in my script, then the maxDist works, but the new target is on a different angle than where I clicked. If anything is unclear, please leave a comment.

Answer this question