在 roblox studio 中如何检测光线投射是否击中玩家?

问题描述 投票:0回答:1
local PathfindingService = game:GetService("PathfindingService")


local Humanoid = script.Parent.Humanoid
local Root = script.Parent.HumanoidRootPart
local goal = script.Parent.Goal -- Change the name of the goal to your Part that you want it to reach.
local prompt = game.Workspace.KickDoor.Door.KickPromptAttachment.ProximityPrompt


prompt.Triggered:Connect(function(plr)
    local playerhumanoid = plr.Character:WaitForChild("Head")
    local path = PathfindingService:CreatePath()
    path:ComputeAsync(Root.Position, goal.Position)
    local waypoints = path:GetWaypoints()
    for i, waypoint in ipairs(waypoints) do
        Humanoid:MoveTo(waypoint.Position)
        if waypoint.Action == Enum.PathWaypointAction.Jump then
            Humanoid.Jump = true
        end
        local RunService = game:GetService("RunService")
        Humanoid.MoveToFinished:Connect(function()
            RunService.Heartbeat:Connect(function()
                Root.CFrame = CFrame.lookAt(
                    Root.Position,
                    playerhumanoid.Position * Vector3.new(1,0,1)
                        + Root.Position * Vector3.new(0,1,0))
                while true do
                    local a = script.Parent.Head
                    local b = playerhumanoid

                    local direction = b.position - a.Position

                    local raycastResult = workspace:Raycast(a.Position,direction)

                    if raycastResult and raycastResult.Instance then

                        raycastResult.Instance.Color = Color3.new(1,0,0)
                        
                    end
                    task.wait(1)
                end
            end)
        end)
    end
end)

Humanoid.Died:Connect(function()
    local ragdoll = game.ReplicatedStorage.Ragdoll
    local clonedModel = ragdoll:Clone()
    clonedModel.Parent = workspace
    clonedModel:MoveTo(Humanoid.Parent.HumanoidRootPart.Position-Vector3.new(0,-5.5,0))
    wait(.1)
    Humanoid.Parent:Destroy()
end)

如何检测来自 NPC 的光线投射是否击中玩家?目前,脚本检测玩家和 NPC 之间的部分,并更改检测到的部分的颜色。所需的功能是,当玩家不在遮挡 NPC 视野的部分后面时,NPC 会检测到玩家。

lua roblox raycasting roblox-studio
1个回答
0
投票

您可以检查

RaycastResult
的祖先是否有
Humanoid
以确定您的
Raycast
是否已击中
Player

由于每个玩家的

Character
都是
Model
并且有一个
Humanoid
孩子,如果您的光线投射击中带有
Humanoid
的模型,我们可以放心地假设我们击中了玩家的角色。

从那里您可以使用

Players:GetPlayerFromCharacter
从其
Player
中获取
Character

if raycastResult and raycastResult.Instance then
    raycastResult.Instance.Color = Color3.new(1, 0, 0)

    -- The instance hit will be a child of a character model
    -- If a humanoid is found in the model then it's likely a player's character
    local characterModel = raycastResult.Instance:FindFirstAncestorOfClass("Model")
    if characterModel then
        local humanoid = characterModel:FindFirstChild("Humanoid")
        if humanoid then
            print("Detected " .. characterModel.Name)
        end
    end
end

上面的代码将检查

Raycast
是否有一个
Model
的祖先,然后检查该模型是否有
Humanoid
。如果我们这样做,在这种情况下,打印我们检测到玩家以及玩家的名字。

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