Unable To Get Directions Or Set Directly

So I Wanted To Make A Sword And It Face Upward Yet. So When I Try Set The Lookvector Or Set The Look. It Set It Self Not In The Upward(Blade) Direction. I’ve Try Scripted But Not Work. Is Theres Any Ways To Do That?

Sword


I Don’t Know If There Is A Ways…

Here My Script. You Can Check If There Any Issue, And This Is Just A Small Line:

ServerSide Script

local sword = game.ReplicatedStorage.Demind:FindFirstChild("Sword") and game.ReplicatedStorage.Demind.Sword:Clone()
if sword then
    sword.Parent = Workspace
    sword.Position = root.Position - Vector3.new(math.random(-64,64),18,math.random(-64,64))
    sword.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") and Players:GetPlayerFromCharacter(hit.Parent) then
            hit.Parent.Humanoid:TakeDamage(12)
        end
    end)
    local tweenFly = TweenService:Create(sword, TweenInfo.new(0.8), {Position = sword.Position + Vector3.new(0, 24, 0)})
    tweenFly:Play()
    tweenFly.Completed:Wait()

    sword.Script.Enabled = true
    for _ = 1, 480 do
        task.wait()
        if not sword then
            return
        end
        -- build axes so Y points at target
        local yAxis = (getNearestPlayerPosition(root.Position) - sword.Position).Unit
        local arb   = math.abs(yAxis.Y) > 0.99 and Vector3.new(1, 0, 0) or Vector3.new(0, 1, 0)
        local xAxis = arb:Cross(yAxis).Unit
        local zAxis = yAxis:Cross(xAxis)
        sword.CFrame = CFrame.fromMatrix(sword.Position, xAxis, yAxis, -zAxis)
    end

    -- Once rotation done, fling sword to the player pos
    local dir    = (getNearestPlayerPosition(root.Position) or sword.Position).Unit * 640
    local params = RaycastParams.new()
    params.FilterDescendantsInstances = { sword, root.Parent }
    params.FilterType                = Enum.RaycastFilterType.Blacklist
    params.IgnoreWater               = true

    local result      = workspace:Raycast(sword.Position, dir, params)
    local destination = result and result.Position or (sword.Position + dir)

    local flingTween = TweenService:Create(
        sword,
        TweenInfo.new(0.4, Enum.EasingStyle.Quad, Enum.EasingDirection.In),
        { Position = destination }
    )
    flingTween:Play()
    flingTween.Completed:Connect(function()
        if sword then
            sword:Destroy()
        end
    end)
end

I Don’t Know. You Can Help.

I’m not sure if I’m answering your question correctly b/c the English on this post is a little broken, but my understanding of what you’re asking is:

How do I have a cframe face a target with an arbitrary object space direction (i.e. up-vector faces the target)?

To do this I think it’s helpful to think about the final CFrame as two operations combined together.

First, a CFrame that has its look vector facing the target. This can be calculated with CFrame.lookAt or CFrame.lookAlong.

Second, a CFrame that represents the rotational change to get from the look vector to the desired facing direction. For example, to get from the look vector to the up-vector you’d need to rotate 90 degrees around the object’s x-axis.

Calculating this value is also relatively simple, but the constructor for doing so is less known / understood. Use CFrame.fromRotationBetweenVectors(Vector3.zAxis, facingDirection) where the first argument Vector3.zAxis is constant representing the look vector in object space and the second argument can be changed to whatever object space direction you want to face.

local function lookAt(from: Vector3, to: Vector3, direction: Vector3?)
	local rotationalOffset = CFrame.fromRotationBetweenVectors(Vector3.zAxis, direction or Vector3.zAxis)
	return CFrame.lookAt(from, to) * rotationalOffset
end

game:GetService("RunService").Heartbeat:Connect(function()
	sword:PivotTo(lookAt(position, target.Position, Vector3.yAxis))
end)

1 Like

Let Me Try It, Maybe Helpful A. So It A Part. I Mean Mine Is A Part. Not Working On The Fling Part.

FLING


-- Once rotati the sword toward that player’s last known location
local dir    = lookAt(sword.Position, sword.CFrame.LookVector, Vector3.yAxis).LookVector
local params = RaycastParams.new()
params.FilterDescendantsInstances = { sword, root.Parent }
params.FilterType                = Enum.RaycastFilterType.Exclude
params.IgnoreWater               = true

local result      = workspace:Raycast(sword.Position, dir, params)
local destination = result and result.Position or (sword.Position + dir)

local flingTween = TweenService:Create(
    sword,
    TweenInfo.new(0.4, Enum.EasingStyle.Quad, Enum.EasingDirection.In),
    { Position = destination }
)
flingTween:Play()
flingTween.Completed:Connect(function()
    if sword then
        sword:Destroy()
    end
end)