【问题标题】:Roblox Pathfinding Back and forth movement when reaching the destination requires jumpingRoblox Pathfinding 到达目的地时的来回移动需要跳跃
【发布时间】:2021-11-05 14:15:46
【问题描述】:

由于某种原因,当目标目标被放置在需要跳跃才能到达我的 NPC 的某个地方时,我的 NPC 正朝着它移动,同时不断在来回移动之间切换。

代码:

local path = PathfindingService:CreatePath()

local waypoints
local currentWaypointIndex

local function followPath(destinationObject)
    
    path:ComputeAsync(enemyNPC.HumanoidRootPart.Position, destinationObject.Position)

    waypoints = {}

    if path.Status == Enum.PathStatus.Success then
        waypoints = path:GetWaypoints()
        currentWaypointIndex = 1
        humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
    else
        error("Path not really working")
    end
end

local function onWaypointReached(reached)
    if reached and currentWaypointIndex < #waypoints then
        currentWaypointIndex += 1

        if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
            humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
        end

        humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
    end

    if reached and currentWaypointIndex >= #waypoints then
        print("Target reached!")
        animationTrack:Stop()
    end
end

local function onPathBlocked(blockWaypointIndex)
    if blockWaypointIndex > currentWaypointIndex then
        followPath(destination)
    end
end

path.Blocked:Connect(onPathBlocked)

humanoid.MoveToFinished:Connect(onWaypointReached)
followPath(destination)

(我用一些局部变量删除了代码的顶部,因为它不允许我发布整个内容而不说我的帖子主要是代码)

有人知道我做错了什么吗?

【问题讨论】:

    标签: roblox


    【解决方案1】:

    您正在寻找PathWaypoint 中的操作,这是一个PathWaypointAction 枚举。您必须确定 PathWaypoint 是 Walk 还是 Jump 动作。目前,不管它是否必须跳跃,你都在做MoveTo()

    所以,在你的代码中:

    if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
         humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
    else
         ​humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      相关资源
      最近更新 更多