我的武器脚本有效,但是子弹什么也没做

时间:2019-03-10 01:43:33

标签: scripting roblox

所以我用枪支脚本做得很好。除了克隆具有损坏脚本的砖块不起作用外,它没有任何问题。即使当我尝试克隆砖块的其他方法时,它们也无法正常工作。这是脚本的样子。我削减了一些部分以使其更易于阅读

local Velo1 = script.Parent.Parent.Parent.Parent.Parent.Parent.Workspace.Car1.Body.Weapons.MachineGun.Bullet1
local Velo2 = script.Parent.Parent.Parent.Parent.Parent.Parent.Workspace.Car1.Body.Weapons.MachineGun.Bullet2
local dx = math.random(50,50)
local dy = math.random(0,0)
local dz = math.random(0,0)
local mag = math.random(750,750)

function onClicked()
    script.Parent.Parent.Parent.Parent.Parent.Parent.Workspace.Car1.Body.Weapons.MachineGun.Part1.Sound:Play()
    script.Parent.Parent.Parent.Parent.Parent.Parent.Workspace.Car1.Body.Weapons.MachineGun.Part2.Sound:Play()
    local bullet = script.Parent.Parent.Parent.Parent.Parent.Parent.Workspace.Car1.Body.Weapons.MachineGun.Bullet1:Clone()
    bullet.Parent = game.Workspace
    bullet.Transparency = 0
    bullet.Anchored = false
    bullet.Velocity = bullet.CFrame:vectorToWorldSpace(Vector3.new(mag + dx,dy,dz))
    local bullet2 = script.Parent.Parent.Parent.Parent.Parent.Parent.Workspace.Car1.Body.Weapons.MachineGun.Bullet2:Clone()
    bullet2.Parent = game.Workspace
    bullet2.Transparency = 0
    bullet2.Anchored = false
    bullet2.Velocity = bullet.CFrame:vectorToWorldSpace(Vector3.new(mag + dx,dy,dz))
end
script.Parent.MouseButton1Down:connect(onClicked)

1 个答案:

答案 0 :(得分:0)

从表面上看,您想制作一种游戏,其中砖头在射击时会受到伤害,但由于某些原因它们并未受到伤害。我在调试时会寻找的东西是尝试回答以下问题:

1)项目符号会在我想要的时间和地点生成吗?

2)子弹方向正确吗?

3)砖头是否检测到已经被子弹碰过?

您的代码示例仅提供有关问题1和2的线索。一个问题可能是您的子弹速度计算是沿您不期望的方向发送子弹。

在LocalScript中尝试类似这样的操作,以确保创建并移动项目符号:

-- alias some variables to make it easier to find things
local fireButton = script.Parent -- <-- assumed that this is a button
local carWorkspace = script.Parent.Parent.Parent.Parent.Parent.Parent.Workspace
local car1 = carWorkspace.Car1
local machineGun = car1.Body.Weapons.MachineGun

-- create some constants for math stuff
local bulletSpeed = 30.0 -- <-- this is super slow so that we can test it
local counterGravityVect = Vector3.new(0, 93.2, 0) -- <-- this is witchcraft

-- make a helper for cloning bullets
local function createBullet(templateBullet, targetVector)
    local newBullet = templateBullet:Clone()
    newBullet.Transparency = 0
    newBullet.Anchored = false
    newBullet.Parent = game.Workspace

    -- use a BodyForce instead of Velocity to propel the bullet
    -- NOTE - there's no clear documentation about how much force you need
    --        just mess with bulletSpeed until it moves fast enough for you
    newBullet.Massless = true
    local targetForce = BodyForce.new()
    targetForce.Force = counterGravityVect + (targetVector * bulletSpeed)
    targetForce.Parent = newBullet

    return newBullet
end 

function fireGun()
    print("Gun fired, creating bullets...)

    -- play the bang sounds
    machineGun.Part1.Sound:Play()
    machineGun.Part2.Sound:Play()

    -- get the orientation of the machine gun
    local targetVect = machineGun.CFrame.LookVector

    -- spawn two new bullets and fire them
    -- in the same direction as the machine gun
    local b1 = createBullet(machineGun.bullet1, targetVect)
    local b2 = createBullet(machineGun.bullet2, targetVect)
end

-- listen for mouse or touch input to the
fireButton.Activated:Connect(fireGun)

但是您还需要确保您的积木在碰到时会损坏。这是执行此操作的简单方法:

1)创建零件

2)将NumberValue添加为子项

3)将脚本添加到如下所示的部分:

print("Hello world!- wall")
local wall = script.Parent
local health = wall.Health
local maxHealth = 10

-- top off the health
health.Value = maxHealth

-- add an event listener to the wall to make sure that it can get shot
wall.Touched:Connect(function(toucher)
    print("Wall Touched ", toucher)
    if (toucher.Name == "bullet") then
        print("Got shot, losing health : ", health.Value - 1)
        health.Value = health.Value - 1
        toucher:Destroy()

        if health.Value <= 0 then
            wall:Destroy()
        end
    end
end)

我希望这已经涵盖了可能出问题的基础。让我知道这是否没有道理。祝你好运!