Problem with CFrame.LookAt

I am trying to make a beam that follows the mouse but the part keeps going inverted, its going the opposite direction also.

Code

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local Modules = ReplicatedStorage.Modules
local Remotes = ReplicatedStorage.Remotes

local Replicator = Modules.Replicator
local Skills = Replicator.Skills

local Particles = script.Parent

local Cryogenic = {}

function Cryogenic.Ability(Player : Player)
	local Assets = script.Assets

	local Character = Player.Character
	local Humanoid = Character.Humanoid
	local RootPart = Character.HumanoidRootPart
	
	if not Humanoid or Humanoid.Health == 0 then return end
	
	local Mouse = Player:GetMouse()
	
	local BeamAbility = Assets.Beams:Clone()
	BeamAbility.Parent = workspace.FX
	BeamAbility.CFrame = RootPart.CFrame * CFrame.new(3,4,0) * CFrame.Angles(math.rad(90),0,0)
	
	while task.wait() do
		BeamAbility.CFrame = CFrame.lookAt(BeamAbility.Position, Mouse.hit.Position)
	end
	
	task.delay(4.4, function()
		Cryogenic.ToggleBeams(BeamAbility, false)
		Cryogenic.ToggleParticles(BeamAbility, false)
	end)
end

function Cryogenic.ToggleBeams(Parent, Boolean : boolean)
	for _, Beams in pairs(Parent:GetDescendants()) do
		if Beams:isA("Beam") then
			Beams.Enabled = Boolean 
		end
	end
end

function Cryogenic.ToggleParticles(Parent, Boolean : boolea)
	for _, Particles in pairs(Parent:GetDescendants()) do
		if Particles:isA("ParticleEmitter") then
			Particles.Enabled = Boolean
		end
	end
end

function Cryogenic.EmitParticles(Parent, Count : number)
	for _, Particles in pairs(Parent:GetDescendants()) do
		if Particles:isA("ParticleEmitter") then
			Particles:Emit(Particles:GetAttribute("EmitCount"))

			if Count then
				Particles:Emit(Count)
			end
		end
	end
end

return Cryogenic

Video : Watch Desktop 2024.05.07 - 19.26.31.08 | Streamable

1 Like

You can try calculating the orientation manually by using Vector3.lookVector to find the direction between the current position and the target position, and then construct a new CFrame using that information.

Mind sending a script example, i already tried something like that?, but i think i have done it wrongly.

Of course I can send a script example. Use this for help

local lookVector = (Mouse.hit.Position - BeamAbility.Position).unit
BeamAbility.CFrame = CFrame.new(BeamAbility.Position, BeamAbility.Position + lookVector)

That did not fix the issues its still like inverted / buggy, needs to point the direction that my mouse is pointing, Watch Desktop 2024.05.07 - 19.26.31.08 | Streamable

I’m sorry it didn’t work the first time. Try this

function Cryogenic.Ability(Player : Player)
    local Assets = script.Assets

    local Character = Player.Character
    local Humanoid = Character.Humanoid
    local RootPart = Character.HumanoidRootPart

    if not Humanoid or Humanoid.Health == 0 then return end

    local Mouse = Player:GetMouse()

    local BeamAbility = Assets.Beams:Clone()
    BeamAbility.Parent = workspace.FX
    BeamAbility.CFrame = RootPart.CFrame * CFrame.new(3, 4, 0) * CFrame.Angles(math.rad(90), 0, 0)

    while task.wait() do
        local lookRay = workspace.CurrentCamera:ViewportPointToRay(Vector2.new(Mouse.X, Mouse.Y))
        local hitPosition = lookRay.origin + lookRay.direction * 100 -- Adjust the multiplier as needed

        BeamAbility.CFrame = CFrame.new(BeamAbility.Position, hitPosition)
    end

    task.delay(4.4, function()
        Cryogenic.ToggleBeams(BeamAbility, false)
        Cryogenic.ToggleParticles(BeamAbility, false)
    end)
end

the calculation of the hitPosition based on the mouse cursor position has been integrated using the ViewportPointToRay method.

It says Argument 2 missing or nil at line 30, line 30 is the LookRay Variable.

1 Like

Wait, if that didn’t work than use this.

function Cryogenic.Ability(Player: Player)
    local Assets = script.Assets
    local Character = Player.Character
    local Humanoid = Character:FindFirstChild("Humanoid")
    local RootPart = Character:FindFirstChild("HumanoidRootPart")

    if not Humanoid or Humanoid.Health == 0 then
        return
    end

    local Mouse = Player:GetMouse()

    local BeamAbility = Assets.Beams:Clone()
    BeamAbility.Parent = workspace.FX
    BeamAbility.CFrame = RootPart.CFrame * CFrame.new(3, 4, 0)

    while task.wait() do
        local lookRay = workspace.CurrentCamera:ViewportPointToRay(Vector2.new(Mouse.X, Mouse.Y))
        local hitPosition = lookRay.origin + lookRay.direction * 100

        BeamAbility.CFrame = CFrame.new(BeamAbility.Position, hitPosition)
    end

    task.delay(4.4, function()
        Cryogenic.ToggleBeams(BeamAbility, false)
        Cryogenic.ToggleParticles(BeamAbility, false)
    end)
end

This has the same error from the previous script.

I’m sorry that happened to you. Try this:

function Cryogenic.Ability(Player: Player)
    local Assets = script.Assets
    local Character = Player.Character
    local Humanoid = Character:FindFirstChild("Humanoid")
    local RootPart = Character:FindFirstChild("HumanoidRootPart")

    if not Humanoid or Humanoid.Health == 0 then
        return
    end

    local Mouse = Player:GetMouse()

    local BeamAbility = Assets.Beams:Clone()
    BeamAbility.Parent = workspace.FX
    BeamAbility.CFrame = RootPart.CFrame * CFrame.new(3, 4, 0)

    while task.wait() do
        local viewportPoint = Vector2.new(Mouse.X, Mouse.Y)
        local lookRay = workspace.CurrentCamera:ViewportPointToRay(viewportPoint)
        
        if lookRay then
            local hitPosition = lookRay.origin + lookRay.direction * 100
            BeamAbility.CFrame = CFrame.new(BeamAbility.Position, hitPosition)
        end
    end

    task.delay(4.4, function()
        Cryogenic.ToggleBeams(BeamAbility, false)
        Cryogenic.ToggleParticles(BeamAbility, false)
    end)
end

Same exact error from last script.

I’m very sorry but I don’t think I will be able to help you any further since I do not know what to do. I wish you luck on finding the solution you want.

Its alright thanks for helping me!

Try using trigonometric functions to calculate the rotation:

while task.wait() do
	local mousePos = Mouse.Hit.Position
	local characterPos = RootPart.Position
	local direction = (mousePos - characterPos).unit
	
	-- calculate the rotation to look at the mouse position
	local lookAtCFrame = CFrame.new(characterPos, mousePos)
	local rotation = CFrame.new(Vector3.new(), direction)
	
	-- apply the rotation
	BeamAbility.CFrame = lookAtCFrame * rotation
end