My bullets are curving for no apparent reason?

I’ve made a gun which spawns a transparent part with a yellow trail to make interesting looking bullets. The only issue i have is that when I shoot the gun, the bullet curves at a big angle and then curves back to the mouse target. This has never happened before? Heres a clip of my friend experiencing this issue.

https://gyazo.com/0577d4be1ce75fe08b136e7722a5334a

As you can see, when my friends mouse was on the sky, the bullet performed as intended. However, when he tried shooting at a wall, the bullets make a very noticable curve. I have no idea what’s causing this. Heres my code:

local glockpos = glock.Attachment.WorldPosition
local mousepos = mouse.Hit.p

local bullet = Instance.new("Part",workspace)
local at0 = Instance.new("Attachment", bullet)
at0.Name = "AT0"
at0.Position = Vector3.new(0, 0.125, 0)
local at1 = Instance.new("Attachment", bullet)
at1.Name = "AT1"
at1.Position = Vector3.new(0, -0.125, 0)
local trail = Instance.new("Trail", bullet)
trail.Attachment0 = bullet.AT0
trail.Attachment1 = bullet.AT1
trail.Color = ColorSequence.new(Color3.fromRGB(255,255,0))
trail.WidthScale = wep.Handle.Trail.WidthScale
trail.Lifetime = 0.05
trail.FaceCamera = true
trail.TextureMode = Enum.TextureMode.Static
trail.Transparency = NumberSequence.new(0)
trail.Enabled = true
bullet.Name = "Bullet"
bullet.Material = Enum.Material.Neon
bullet.Size = Vector3.new(1,1,1)
bullet.Anchored = false
bullet.CanCollide = false
bullet.Transparency = 1
bullet.Color = Color3.fromRGB(255,255,0)
bullet.CFrame = CFrame.new(glockpos, mousepos)
local bv = Instance.new("BodyVelocity", bullet)
bv.Velocity = bullet.CFrame.lookVector * 820
debris:AddItem(bullet, 5)

This causes problems because it can sometimes completely miss the target. Does this have something to do with the trail or attachment on the bullet?

4 Likes

The only reason for the curve I could think of would be a visual glitch of your trails.
Since you are using position instead of cframe, the attachment is being moved back in world space. I have never run into this glitch so keep me updated.
Good luck!

(This has nothing to do with it but I would use raycasting for the endpoint of the physics calculation just so it doesnt try and go through walls)

2 Likes

Almost definitely has to do with the way you’re setting the BodyVelocity. That would certainly cause a curve, as would using the incorrect relativity for the bullet. Try revising how you create it or look into VecrorForce.

By the way, stop using the parent argument of Instance.new. You’re registering items into the DataModel before they’ve been fully initialised with all the other property sets so this code is less performant to run. You should be running in this order:

Create part, set part properties, create attachments, set attachment properties, parent attachments, create trail, set trail properties, parent trail, create BodyVelocity, parent BodyVelocity, parent part.

1 Like

If all fails, I might try this method. Could you tell me how to do that? I dont know how Raycasting works.

Probably the Best way to figure( considering that you haven’t already figured it out by the time I posted this ) that particular question out is to search it up, Raycasting is the cast of a Ray (a half finite and infinite line) in any given direction, it’s commonly used in things such as collsion detection and bullet projectiles. in roblox a Ray can be created using the Ray.new(vector3, direction) constructer and can be used to create many things including guns as seen In this tutorial: Documentation - Roblox Creator Hub

The tutorial is describing making a laser gun, but if you did use ray casting you would probably just generate a ray then have bullets follow the ray( if that makes sense)

Also I agree with Colbert, on that the issue probably has something to do with the body velocity. As a side thing I personally wouldn’t use body movers( in some cases for this), just because I feel as though there aren’t always reliable, sometimes there are just a lot of factors that can affect the physics/projectile of an object (just an opinion, this is probably not fact!)

Nice animation Ui thing under the player, I like it :smirk: , your game( if this is a game) looks pretty cool when is it going to be out?

1 Like

How do you make a part that follows along a rays path? I searched far and wide for tutorials on YouTube and didnt get what i needed.

1 Like

It’s quite simply especially because rays are really just lines, If you were to do ray casting, first thing you need to do is determine where the end and start positions are, in this case i am going to use parts that i placed in the work space, likely you will want to use mouse.Hit for the end position:

local StartPos = game.Workspace.Startpart.Position
local Endpos = game.Workspace.Endpart.Position

then we need to actually create the ray, for this we are going to put StartPos for our position (Vector3) and (Endpos -StartPos).unit Multiplied times 300 for the direction( There are probably other things you could multiply it by, but for now this will work ):

local ray = Ray.new(StartPos, (Endpos -StartPos).unit * 300)

Next, we can create our part or bullet (just using a normal part as an example):

local Part = Instance.new("Part")
Part.Parent = game.Workspace
Part.Anchored =true

Set it to the start position:

Part.Position = StartPos

Then Just Follow the ray by going to the End Position of the ray ( start to end):
To find the end of the ray we would use this: ray.Origin + distance * ray.Direction.unit

--( i am using The TweenService for this example, you can use CFrame or whatever)
local TweenInformation = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local Tween =  TweenService:Create(Part, TweenInformation, {CFrame = CFrame.new(ray.Origin + distance * ray.Direction.unit)})
Tween:Play() -- play the tween

Now if we put that all together, we should/can get something like this:

i created a beam, illustrating the ray, for visualization purposes!

( i used concepts that were mentioned in the above resources (particularity the laser gun one) )

Full Test Script
----local script!
local StartPos = game.Workspace.Startpart.Position
local Endpos = game.Workspace.Endpart.Position
local TweenService = game:GetService("TweenService")

local ray = Ray.new(StartPos, (Endpos -StartPos).unit * 300)


local beam = Instance.new("Part", workspace) -- create a beam for visualization 
beam.BrickColor = BrickColor.new("Bright red")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
		
local distance = (StartPos - Endpos ).magnitude -- Get the magnitude from the  StartPos  to Endpos( for visualization )
beam.Size = Vector3.new(0.3, 0.3, distance) -- Set the beams size
beam.CFrame = CFrame.new( StartPos , Endpos) * CFrame.new(0, 0, -distance / 2) -- Set the beam's CFrame


local function TravesePartAlongBeam()
	local Part = Instance.new("Part")
	Part.Parent = game.Workspace
	Part.Anchored =true
	
	Part.Position = StartPos
	
	------ I am using the tween service for an example you can of course use CFrames
	local TweenInformation = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
      local Tween =  TweenService:Create(Part, TweenInformation, {CFrame = CFrame.new(ray.Origin + distance * ray.Direction.unit)})
	Tween:Play() -- play the tween
	


	
end


while true do -- loop to create more parts
	wait(1)
TravesePartAlongBeam() 
end


Note: This was just a example of how to get a part to follow are Ray, you would have to add other things for it work with a gun( Mouse.hit, “Click to Send Ray”, etc.)

1 Like

I know its odd to post a solution to my own post. I figured out what caused the bullets to make groovy movements when spawned.

Incase you wanted to know, gravity and other things acted before the BodyVelocity did. The solution to this was to set the MaxForce to Vector3.new(math.huge, math.huge, math.huge). My bullets now work as intended. Thank you to everyone that replied!

2 Likes