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

How to Ignore Y and X Axis in CFrame?

Asked by 6 years ago

I'm making a script where when the player gets near the NPC, the NPC looks at the player. It works but I want it to ignore the Y and X axis.

script.Parent.Touched:connect(function(Char)

    local Humanoid = Char.Parent:FindFirstChild("Humanoid")

    if Humanoid ~= nil then

    local torso = Char.Parent:FindFirstChild("HumanoidRootPart")

    while wait() do
            script.Parent.Parent.HumanoidRootPart.CFrame = CFrame.new(script.Parent.Parent.HumanoidRootPart.Position, torso.Position)
            end
            end
end)

How do I go about doing this?

1 answer

Log in to vote
0
Answered by 6 years ago

I think what you are really asking is for is how to set the "look at" position so that it's the same height as the NPC's HumanoidRootPart. That way, we can "look at" a Position, while only taking into account its X and Z coordinates, and we won't topple over our NPC.

Here is your code with the above change, plus some clean-up

script.Parent.Touched:connect(function(Char)
    local Humanoid = Char.Parent:FindFirstChild("Humanoid")
    if Humanoid ~= nil then
        local torso = Char.Parent:FindFirstChild("HumanoidRootPart")
        local rotatingPart = script.Parent.Parent.HumanoidRootPart
        while wait() do
            local lookAt = Vector3.new(
                torso.Position.X,
                rotatingPart.Position.Y,
                torso.Position.Z
            )
            rotatingPart.CFrame = CFrame.new(rotatingPart.Position, lookAt)
        end
    end
end)
0
It works in Studio but in-game, it just disappears WaddelsG 69 — 6y
0
Nvm fixed it tysm WaddelsG 69 — 6y
Ad

Answer this question