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.

01local tool = script.Parent
02local player = tool.Parent.Parent
03local mouse = player:GetMouse()
04local root = player.Character.HumanoidRootPart
05 
06 
07tool.RemoteEvent.OnServerEvent:connect(function(player,mousehit)
08    if player.Character:FindFirstChild("multiattackprevention") then return end
09    local multiattackprevention = Instance.new("StringValue")
10    multiattackprevention.Parent = player.Character
11    multiattackprevention.Name = ("multiattackprevention")
12    local hitcooldown = false
13    local animation = player.Character.Humanoid:LoadAnimation(player.Character:FindFirstChild("SuperJump"))
14    animation:Play()
15    wait(0.5)
View all 67 lines...

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

The error is to do with:

1bv.Velocity =  root.CFrame.p * 150

I know this because when I put:

1bv.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:

1bv.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.

1bv.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.

1local bv = Instance.new("BodyVelocity", player.Character.Torso)
2 
3--Instead of
4 
5local bv = Instance.new("BodyVelocity")
6bv.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
1projectile.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.

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

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

1Projectile.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.

001--Defines
002local tool = script.Parent
003local fire = tool.Fire
004local remote = tool:WaitForChild("RemoteEvent")
005 
006--Settings
007local damage = 40 -- Headshot damage is * 2.5
008local bulletSpeed = 500 -- Don't go for too much please
009 
010--Startup
011if tool.Parent:IsA("Model") then
012    if game.Players:GetPlayerFromCharacter(tool.Parent) then
013        for i,v in pairs(tool:GetChildren()) do
014            if v:IsA("Part") or v:IsA("BasePart") then
015                v.CanCollide = false
View all 131 lines...

It's unfinished so don't try.

Answer this question