Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make this projectile fire in the right direction?

Asked by 7 years ago

I am very new to scripting so I'm sorry if this scripts untidiness makes you cringe.

local tool = script.Parent
local player = tool.Parent.Parent
local mouse = player:GetMouse()
local root = player.Character.HumanoidRootPart


tool.RemoteEvent.OnServerEvent:connect(function(player,mousehit)
    if player.Character:FindFirstChild("multiattackprevention") then return end
    local multiattackprevention = Instance.new("StringValue")
    multiattackprevention.Parent = player.Character
    multiattackprevention.Name = ("multiattackprevention")
    local hitcooldown = false
    local animation = player.Character.Humanoid:LoadAnimation(player.Character:FindFirstChild("SuperJump"))
    animation:Play()
    wait(0.5)
    local bv = Instance.new("BodyVelocity")
    bv.Parent = player.Character.Torso
    bv.Velocity = Vector3.new(0,200,0)
    wait(0.7)
    bv:Destroy()
    root.CFrame = mouse.Origin
    player.Character.Head.Anchored = true
    local animation = player.Character.Humanoid:LoadAnimation(tool.Cast)
    animation:Play()
    wait(0.4)
    local projectile = Instance.new("Part")
    projectile.Transparency = 1
    projectile.Parent = game.Workspace
    projectile.Position = player.Character:FindFirstChild("Right Arm").Position
    projectile.CanCollide = false
    projectile.TopSurface = ("Smooth")
    projectile.BottomSurface = ("Smooth")
    projectile.Size = Vector3.new(10,10,10)
    local fireparticle = Instance.new("Fire")
    fireparticle.Parent = projectile
    fireparticle.Size = 20
    fireparticle.Heat = 25
    local bv = Instance.new("BodyVelocity")
    bv.Parent = projectile  
    bv.MaxForce = Vector3.new(1e8,1e8,1e8)  
    bv.Velocity =  root.CFrame.p * 150
    local sound = Instance.new("Sound")
    wait(0.2)
    multiattackprevention:Destroy()
    projectile.CanCollide = true
    player.Character.Head.Anchored = false
    projectile.Touched:connect(function(hit)
        projectile.Anchored = true
        fireparticle.Size = 30
        bv.Velocity = mousehit.lookVector * 0
        if hitcooldown == true then return end
        local enemy = hit.Parent:FindFirstChild("Humanoid")
        if enemy == player.Character.Humanoid then return end
        if enemy then
            enemy.Health = enemy.Health - 250
            hitcooldown = true
            wait(1)
            projectile:Destroy()
        else wait(1)
            projectile:Destroy()
            wait(3)
        end
    end)    
    wait(10)
    projectile:Destroy()
    multiattackprevention:Destroy()
end)    

This works with no output error, however the projectile doesn't spawn.

The error is to do with:

bv.Velocity =  root.CFrame.p * 150

I know this because when I put:

bv.Velocity = mousehit.lookVector * 150

the projectile fires, just in the wrong direction.

What should I make bv.Velocity equal to?

0
use raycasting!1!! stop using terrible roblox physics!!1 hiimgoodpack 2009 — 7y
0
bv.Velocity = (root.Position - mousehit.p).unit * 150. Normalise the two vectors then multiply by an arbitrary speed (in this case 150) LifeInDevelopment 364 — 7y

3 answers

Log in to vote
1
Answered by 7 years ago

If you want your projectile to fire in the direction the player's torso is facing, which is what it seems like, you were very close. When you had:

bv.Velocity =  root.CFrame.p * 150

The projectile DID spawn, it was just moving so quickly you couldn't catch a glimpse of it, because you used position, which is usually a large value, so your projectile went shooting off at the speed of sound. What you want is lookVector, which is the unit vector of the direction a CFrame is facing.

bv.Velocity =  root.CFrame.lookVector * 150

Enjoy :)

Also, a tip: When using Instance.new(), you can assign a parent instantly, without using another line. Just put a comma, and then what you want to parent the new object to.

 local bv = Instance.new("BodyVelocity", player.Character.Torso)

--Instead of 

local bv = Instance.new("BodyVelocity")
bv.Parent = player.Character.Torso

0
Thank you very much! Citranix 19 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Here's an idea

  1. Make the projectile face to where the mouse is
projectile.CFrame = CFrame.new(projectile.CFrame,(mouse.hit.p.x,mouse.hit.p.y,mouse.hit.p.z))
  1. Make the BodyVelocity go to where the projectile is facing
Log in to vote
0
Answered by
SCP774 191
6 years ago
Edited 6 years ago

If you want the BodyVelocity's velocity to "face" your mouse.

Projectile.MaxForce = Vector3.new(1e8,1e8,1e8)
Projectile.Velocity = (mouse.hit.p - root.Position).unit * bulletSpeed

To make the projectile face your mouse. (Makes the projectile look nicer)

Projectile.CFrame = CFrame.new(Projectile.Position, mouse.hit.p)

You can get the mouse using the player got from the RemoteEvent.

Here's my gun's script. Don't blame me for untidy lines, I just wanted to make it quick.

--Defines
local tool = script.Parent
local fire = tool.Fire
local remote = tool:WaitForChild("RemoteEvent")

--Settings
local damage = 40 -- Headshot damage is * 2.5
local bulletSpeed = 500 -- Don't go for too much please

--Startup
if tool.Parent:IsA("Model") then
    if game.Players:GetPlayerFromCharacter(tool.Parent) then
        for i,v in pairs(tool:GetChildren()) do
            if v:IsA("Part") or v:IsA("BasePart") then
                v.CanCollide = false
            end
        end
    else
        for i,v in pairs(tool:GetChildren()) do
            if v:IsA("Part") or v:IsA("BasePart") then
                v.CanCollide = true
            end
        end
    end
else
    for i,v in pairs(tool:GetChildren()) do
        if v:IsA("Part") or v:IsA("BasePart") then
            v.CanCollide = true
        end
    end
end
--CanCollide stuff
tool.AncestryChanged:Connect(function()
    if tool.Parent:IsA("Model") then
        if game.Players:GetPlayerFromCharacter(tool.Parent) then
            for i,v in pairs(tool:GetChildren()) do
                if v:IsA("Part") or v:IsA("BasePart") then
                    v.CanCollide = false
                end
            end
        else
            for i,v in pairs(tool:GetChildren()) do
                if v:IsA("Part") or v:IsA("BasePart") then
                    v.CanCollide = true
                end
            end
        end
    else
        for i,v in pairs(tool:GetChildren()) do
            if v:IsA("Part") or v:IsA("BasePart") then
                v.CanCollide = true
            end
        end
    end
end)

--Functions
local function getChildrenWithClassProvided(index, class)
    for i,v in pairs(index:GetChildren()) do
        if v:IsA(class) then
            return v
        end
    end
end

local function onRemoteFired(plr, cmd, value1, value2)
    if cmd == "fire" then
        -- value1 is end point, value2 is the spread
        -- Yay realistic bullet xd
        local root = plr.Character.HumanoidRootPart
        local startPos = fire.Position
        local bullet = Instance.new("Part")
        bullet.Parent = game.Workspace
        --Properties
        bullet.Anchored = true
        bullet.CanCollide = false
        bullet.BrickColor = BrickColor.new("New Yeller")
        bullet.Material = Enum.Material.Neon
        bullet.Size = Vector3.new(0.25, 0.25, 2.5)
        bullet.CFrame = CFrame.new(startPos, value1)
        bullet.Transparency = 1
        bullet.Name = "Bullet"
        --Realistic effects
        fire.smoke.Enabled = true
        fire.flash.Enabled = true
        fire.light.Enabled = true
        wait(0.1)
        fire.smoke.Enabled = false
        fire.flash.Enabled = false
        fire.light.Enabled = false
        bullet.Transparency = 0
        bullet.Anchored = false
        local bulletLight = Instance.new("PointLight")
        bulletLight.Brightness = 10
        bulletLight.Range = 6
        bulletLight.Color = Color3.new(255, 255, 127)
        bulletLight.Enabled = true
        bulletLight.Shadows = true
        bulletLight.Parent = bullet
        bulletLight.Name = "Light"
        --Movement
        --A lot of physic and math stuff that will get your brain explode
        local mouse = plr:GetMouse()
        local direction = (value1 - fire.Position).unit
        local spawnPos = fire.Position + direction * 2
        bullet.Velocity = direction * 100 -- yay more math
        bullet.CFrame = CFrame.new(bullet.Position, mouse.hit.p)
        local bodyForce = Instance.new("BodyVelocity")
        bodyForce.Parent = bullet
        bodyForce.MaxForce = Vector3.new(1e8,1e8,1e8)
        bodyForce.Velocity = (mouse.hit.p - root.Position).unit * bulletSpeed
        bullet.Touched:Connect(function(part)
            if part.Parent:IsA("Model") then
                local enemyhum = getChildrenWithClassProvided(part.Parent, "Humanoid")
                if enemyhum then
                    if enemyhum ~= tool.Parent.Humanoid then
                        local dmgpt = damage
                        if part.Name == "Head" or part.Parent:IsA("Accessory") then -- That hurt .-.
                            dmgpt = dmgpt * 2.5 -- Instant kill
                        end
                        enemyhum:TakeDamage(dmgpt)
                        bullet:Destroy()
                    end
                end
            end
        end)
       game:GetService("Debris"):AddItem(bullet, 2.5) -- Remove bullet if it's here too long
    end
end

remote.OnServerEvent:Connect(onRemoteFired)

It's unfinished so don't try.

Answer this question