Мой сценарий оружия работает, но пули ничего не делают - PullRequest
0 голосов
/ 10 марта 2019

Так что я отлично справлялся со скриптами с оружием. С этим не было никаких проблем, за исключением того, что кирпичи со сценариями повреждения не работали при клонировании. Даже когда я пробовал альтернативные способы клонировать кирпичи, они не работали. Вот как выглядит сценарий. Я сократил некоторые части, чтобы было легче читать

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 голосов
/ 10 марта 2019

Из того, что это звучит, вы хотите создать игру, в которой кирпичи получают урон при выстреле, но по какой-то причине они не получают урон. То, что я бы искал при отладке, это попытка ответить на следующие вопросы:

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)

Надеюсь, это несколько охватило основы того, что может пойти не так. Дайте мне знать, если что-то из этого не имеет смысла. Удачи!

...