Roblox 击退跳过航路点

问题描述 投票:0回答:1

我正在为 Roblox 塔防游戏创建一个击退脚本,该脚本旨在通过 5 个螺柱将敌人向后传送到另一个部分。击退功能可用于此目的,但在被击退后,敌人会跳过其前进的路径点。该游戏中的敌人通过从一个航路点块移动到另一个航路点块来沿着轨道移动。当击退发生时,敌人会从轨道对角线移动到下一个路径点,而不是它应该移动到的路径点。

到目前为止,我已经尝试使用 BindableEvent 将敌人的航路点目标向后设置 1,但它对其切角运动没有任何影响。这是我的击退脚本的代码:

task.wait(10)
local target = game:GetService("Workspace")["Live Enemies"]["Handcuff Arrest Officer"]

local waypoint: Part = nil
if target.EnemyInfo.Waypoint.Value == 0 then
    waypoint = game:GetService("Workspace").SpawnPoint
else
    waypoint = game:GetService("Workspace").Waypoints[target.EnemyInfo.Waypoint.Value]
end
local studsToMove = 5

-- Make sure the model has a PrimaryPart set
if target.PrimaryPart then
    -- Calculate direction from target's PrimaryPart to waypoint
    local direction = (waypoint.Position - target.PrimaryPart.Position).unit

    -- Calculate the new position, moving the PrimaryPart 5 studs in the direction of the waypoint
    local newPosition = target.PrimaryPart.Position + direction * studsToMove

    -- Offset the entire model by the same movement, preserving its orientation
    target.Knockback:Fire()
    target:SetPrimaryPartCFrame(target.PrimaryPart.CFrame + (direction * studsToMove))
end

另外,这里是敌人移动脚本的代码。它使用来自

https://create.roblox.com/docs/reference/engine/classes/Humanoid
. 的无超时版本 humanoid:MoveTo()

-- In case the enemy dies before this script can run, pcall blocks errors.
pcall(function(...)
    -- Constants
    local humanoid = script.Parent.Humanoid
    local info = script.Parent.EnemyInfo
    local waypoints = game:GetService("Workspace").Waypoints
    local waypoints_hit = 0 -- Not constant, waypoint counter
    local knockback = script.Parent.Knockback
    
    -- MoveTo() without timeout
    local function moveTo(humanoid, targetPoint)
        local targetReached = false
        local connection
        connection = humanoid.MoveToFinished:Connect(function(reached)
            targetReached = true
            print("Reached target "..tostring(targetPoint))
            connection:Disconnect()
            connection = nil
        end)
        humanoid:MoveTo(targetPoint)
        while not targetReached do
            if not (humanoid and humanoid.Parent) then
                break
            end
            if humanoid.WalkToPoint ~= targetPoint then
                break
            end
            humanoid:MoveTo(targetPoint)
            task.wait(0.1)
        end
        if connection then
            connection:Disconnect()
            connection = nil
        end
    end
    
    knockback.Event:Connect(function(...: any)
        print("Fired")
        waypoints_hit -= 1
        script.Parent.EnemyInfo.Waypoint.Value = tostring(waypoints_hit)
        if waypoints_hit == 0 then
            moveTo(humanoid, game:GetService("Workspace").SpawnPoint.Position)
        else
            moveTo(humanoid, game:GetService("Workspace").Waypoints[waypoints_hit].Position)
        end
    end)

    -- Enemy configurations
    local damage: number = info.Damage.Value -- Damage to base
    local hp: number = info.HP.Value -- Hit points
    local speed: number = info.Speed.Value -- Speed
    
    -- Set HP and speed
    humanoid.MaxHealth = hp
    humanoid.Health = hp
    humanoid.WalkSpeed = speed
    
    -- Move
    for i,v in pairs(waypoints:GetChildren()) do
        moveTo(humanoid, v.Position)
        waypoints_hit += 1
        script.Parent.EnemyInfo.Waypoint.Value = tostring(waypoints_hit)
        if waypoints_hit >= 8 then
            game:GetService("Workspace")["Match Config"].BaseHP.Value -= damage
            script.Parent:Destroy()
        end
    end
end)

我发现

humanoid:MoveTo()
当根部分的CFrame改变时停止,这就是我正在做的事情以促进击退。但是,我不确定如何绕过这个限制,我需要为击退功能做到这一点。任何帮助将不胜感激!

lua 3d roblox luau roblox-studio
1个回答
0
投票

您需要在您调用的同一块中调用

:MoveTo
方法来更改您的
CFrame
,例如:

function()
Model.PrimaryPart.CFrame(new cframe here)
Model:MoveTo(cframe here)
end

这允许

:MoveTo
方法正确获取
CFrame
,而不会出现游戏更新问题。

© www.soinside.com 2019 - 2024. All rights reserved.