首页 > 解决方案 > 如何在 Roblox Studio 中找到游戏手柄指针的目标?

问题描述

所以这是一个难题,这个系统,见下文,可以很好地配合键盘和鼠标,但不能很好地配合控制器。我正在尽我最大的努力让游戏至少在 PC 和 Xbox 上可用,而移动和平板电脑是我的下一个目标。游戏本身并不完整,但这并不意味着我不能提前计划。

以下代码位于 Player 的本地脚本中。所有使用的导入和变量都被进一步调用,但暂时被省略以节省空间。

因此,换句话说,我正在使用mouse.Target寻找要攻击的船,这与鼠标配合得很好,Xbox 控制器上的移动效果很好,但是攻击框和选择框在控制器上都不起作用。

我在 Roblox 开发者网站上查看了用户输入服务,但我看不到直接控制器替代方案,我找到了移动选项,但我找不到获取控制器指针目标的方法. 通过控制器指针,我指的是中间的白点,用于使用控制器指导动作

任何帮助表示赞赏,谢谢。

mouse.Move:Connect(function() --When the mouse moves
    if player.Character == nil then
        selection.Adornee = nil     
        return
    elseif player.Character.IsLoading.Value == true or player.Character.InMenu.Value == true then
        --If the user is loading, or in a menu, ignore their mouse movements and clicks.
        return  
    end 
        local target = mouse.Target --Get the target of the "mouse"
        --All of this is just checking to see if it can be selected, and works correctly at this current time with A Mouse, but something is not working here with the controller.
        if not target then
            -- nothing selected
            print ("No target.")
            selection.Adornee = nil         
        elseif (check_if_attackable(target.Parent.Name) == true or target.Parent:FindFirstChild("Health")) and target.Parent ~= player.Character.ShipObject.Value then
            selection.Adornee = target.Parent
    elseif (check_if_attackable(target.Parent.Parent.Name) == true or target.Parent.Parent:FindFirstChild("Health")) and target.Parent.Parent ~= player.Character.ShipObject.Value then
            selection.Adornee = target.Parent.Parent
    elseif (check_if_attackable(target.Parent.Parent.Parent.Name) == true or target.Parent.Parent.Parent:FindFirstChild("Health")) and target.Parent.Parent.Parent ~= player.Character.ShipObject.Value then
            selection.Adornee = target.Parent.Parent.Parent
        
        else
            selection.Adornee = nil
        end
        Target = selection.Adornee
end)


mouse.Button1Up:Connect(function()
    print("Click: UP")
    if attackdebounce == true then
        print ("Attack is cooling down.")
        return
            
    end
    if player.Character.ShipObject.Value == nil then
        print("Cannot move or attack, the ship's been destroyed.")
        return
            
    end
    if Target and Target:FindFirstChild("Health") then
        print("Target present")
        attackdebounce = true
        game.ReplicatedStorage.Ship.Fire:FireServer(Target)
        wait(1.25)
        attackdebounce = false
        
    else
        print("No Target, trying to move.") 
        --The movement works fine on both the Mouse and controller.
        if mouse.Target then
            local movementtarget = mouse.Target.Position
            if mouse.Target then
                local movementtarget = Vector3.new(mouse.Hit.p.x,0.6,mouse.Hit.p.z)
                movementdebounce = true
                
                game.ReplicatedStorage.Ship.Move:FireServer(movementtarget)
                wait(1)
                movementdebounce = false
            end             
        end
    end
end)

标签: luaroblox

解决方案


推荐阅读