敌人的敌人

发布于 2025-01-26 01:01:49 字数 2013 浏览 2 评论 0原文

我有 npc ,大概是敌人。我希望它能返回 原始问题 原始方向 在玩家死亡。 但是(如在代码计算中所标记的),具有函数 :moveto() ,该字符不会返回到实际的相同位置,而是近似位置。例如:

  print(npc.humanoidRootpart.position) - 输出:x:11.5,y:-2.13,z:15.49
打印(原始位置) - 输出:x:11.4923973,y:-2.13432,z:15.49
 

这是我的代码:

local FollowZone = script.Parent
local NPC = FollowZone.Parent

local OriginalOrientation = NPC.HumanoidRootPart.Orientation
local OriginalPosition = NPC.HumanoidRootPart.Position

print(OriginalPosition)
local Fighting = 2 -- 1; No Fighting. 2; Neutral. 3; Fighting

local function OnTouch(Part)
    if Part.Parent:FindFirstChild("Humanoid") ~= nil and tostring(Part.Parent.Parent.Name) ~= "Enemies" then
        local Target = game:GetService("Workspace"):WaitForChild(Part.Parent.Name)
        Fighting = 3

        while Fighting == 3 do
            NPC.Humanoid:MoveTo(Target.HumanoidRootPart.Position)
            
            if Target.Humanoid.Health == 0 then
                Fighting = 1
                BackToPosition()
                BackToOrientation()
            end
            
            wait()
        end
    end
end


function BackToPosition()
    while Fighting == 1 do
        NPC.Humanoid:MoveTo(OriginalPosition)
        
        print(NPC.HumanoidRootPart.Position) -- X: 11.5, Y: -2.13, Z: 15.49
        print(OriginalPosition) -- X: 11.4923973, Y: -2.13432, Z: 15.49 
        
        if NPC.HumanoidRootPart.Position == OriginalPosition then
            Fighting = 2
        end

        wait()
    end
end

function BackToOrientation()
    while NPC.HumanoidRootPart.Orientation ~= OriginalOrientation do
        for Index = 1, OriginalOrientation.X do
            NPC.HumanoidRootPart.Orientation = Vector3.new(Index, OriginalOrientation.Y, OriginalOrientation.Z)
        end
        
        wait()
    end
end


script.Parent.Touched:Connect(OnTouch)

I have NPC which, presumably, is an Enemy. I want it to return to OriginalPosition and OriginalOrientation after the player dies.
However (as marked in a code-comment), with function :MoveTo(), the character doesn't return to the actual same position but an approximate. By example:

print(NPC.HumanoidRootPart.Position) -- Output: X: 11.5, Y: -2.13, Z: 15.49
print(OriginalPosition) -- Output: X: 11.4923973, Y: -2.13432, Z: 15.49

This is my code:

local FollowZone = script.Parent
local NPC = FollowZone.Parent

local OriginalOrientation = NPC.HumanoidRootPart.Orientation
local OriginalPosition = NPC.HumanoidRootPart.Position

print(OriginalPosition)
local Fighting = 2 -- 1; No Fighting. 2; Neutral. 3; Fighting

local function OnTouch(Part)
    if Part.Parent:FindFirstChild("Humanoid") ~= nil and tostring(Part.Parent.Parent.Name) ~= "Enemies" then
        local Target = game:GetService("Workspace"):WaitForChild(Part.Parent.Name)
        Fighting = 3

        while Fighting == 3 do
            NPC.Humanoid:MoveTo(Target.HumanoidRootPart.Position)
            
            if Target.Humanoid.Health == 0 then
                Fighting = 1
                BackToPosition()
                BackToOrientation()
            end
            
            wait()
        end
    end
end


function BackToPosition()
    while Fighting == 1 do
        NPC.Humanoid:MoveTo(OriginalPosition)
        
        print(NPC.HumanoidRootPart.Position) -- X: 11.5, Y: -2.13, Z: 15.49
        print(OriginalPosition) -- X: 11.4923973, Y: -2.13432, Z: 15.49 
        
        if NPC.HumanoidRootPart.Position == OriginalPosition then
            Fighting = 2
        end

        wait()
    end
end

function BackToOrientation()
    while NPC.HumanoidRootPart.Orientation ~= OriginalOrientation do
        for Index = 1, OriginalOrientation.X do
            NPC.HumanoidRootPart.Orientation = Vector3.new(Index, OriginalOrientation.Y, OriginalOrientation.Z)
        end
        
        wait()
    end
end


script.Parent.Touched:Connect(OnTouch)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2025-02-02 01:01:49
local FollowZone = script.Parent
local NPC = FollowZone.Parent

local OriginalPos = NPC.HumanoidRootPart.CFrame
local Fighting = 2 -- 1; No Fighting. 2; Neutral. 3; Fighting

local function OnTouch(Part)
    if Part.Parent:FindFirstChild("Humanoid") ~= nil and tostring(Part.Parent.Parent.Name) ~= "Enemies" then
        local Target = game:GetService("Workspace"):WaitForChild(Part.Parent.Name)
        Fighting = 3

        while Fighting == 3 do
            NPC.Humanoid:MoveTo(Target.HumanoidRootPart.Position)
            
            if Target.Humanoid.Health == 0 then
                Fighting = 1
                BackToOrginalPos()
            end
            
            wait()
        end
    end
end

function BackToOrginalPos()
    NPC.HumanoidRootPart.CFrame = OriginalPos
end


script.Parent.Touched:Connect(OnTouch)
local FollowZone = script.Parent
local NPC = FollowZone.Parent

local OriginalPos = NPC.HumanoidRootPart.CFrame
local Fighting = 2 -- 1; No Fighting. 2; Neutral. 3; Fighting

local function OnTouch(Part)
    if Part.Parent:FindFirstChild("Humanoid") ~= nil and tostring(Part.Parent.Parent.Name) ~= "Enemies" then
        local Target = game:GetService("Workspace"):WaitForChild(Part.Parent.Name)
        Fighting = 3

        while Fighting == 3 do
            NPC.Humanoid:MoveTo(Target.HumanoidRootPart.Position)
            
            if Target.Humanoid.Health == 0 then
                Fighting = 1
                BackToOrginalPos()
            end
            
            wait()
        end
    end
end

function BackToOrginalPos()
    NPC.HumanoidRootPart.CFrame = OriginalPos
end


script.Parent.Touched:Connect(OnTouch)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文