RayCasting wont make the bullet go from the barrel

Hey! I am trying to make a gun that shoots from the barrel, and follows the mouse. The issue is, when I do this, its just shooting from the mouse. Here is a GIF;

Area of code;

return Ray.new(gunTip.CFrame.p, Auto(mousehit.p - gunTip.CFrame.p, math.rad(5)) * (range))
2 Likes

What does Auto() do? We can’t help you without seeing what your code is doing!

Auto() is a function, here is what it does, sorry I forgot to provide it.

function Auto(v) 
  return (CFrame.new(Vector3.new(), v)*CFrame.Angles(0, 0, 0, 2*math.pi)).LookVector 
end

Why is maxAngle passed if it’s unused?

I’ve forgot to remove this, it’s not an issue anyways.

can we see the code for the bullet tracer effect? maybe its just a bug with that code and not one based on how the ray is casted.

Where is mousehit defined? Is it updated every time you click, or is it set first off?

I think it’d be better that you showed the code that’s responsible for creating the tracer rather than the ray creation itself. Sometimes I see that happening - the ray code is fine but the tracer code isn’t. If there’s nothing wrong with the tracer code, my bets are placed on Auto being done wrongly.

Here is the code for the tracer;

    function CreatePartOnPosition(position)
      local part = Instance.new("Part", workspace.Hits)
      part.CanCollide = false
      part.Anchored = true
      part.Position = position
      part.Size = Vector3.new(0.05,0.05,0.05)
      part.Transparency = .4
      local attachment = Instance.new("Attachment", part)
      return part
    end

    function CreateBeam(position, gunMuzzle, guntipPos, speed)
      local gunTip = gunMuzzle:Clone()
      gunTip.Size = Vector3.new(0.05,0.05,0.05)
      gunTip.Parent = workspace.Hits
      gunTip.Transparency = 1
      local connectAttachment = CreatePartOnPosition(position)
      local beam = game.ReplicatedStorage.Beam:Clone()
      beam.Parent = workspace.Hits
      beam.Attachment0 = gunTip.FrontBarrel
      beam.Attachment1 = connectAttachment.Attachment
      local calculateTimeOfShot = (position-guntipPos).magnitude / speed
      local tweenInfo = TweenInfo.new(calculateTimeOfShot, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
      local partProperties = {Position = position}
      local tween = tweenService:Create(gunTip, tweenInfo, partProperties)
      tween:Play()
      tween.Completed:connect(function()
      gunTip:Destroy()
      connectAttachment:Destroy()
      beam:Destroy()
      end)
    end

Here is the area where position is determent

        local hit,position = workspace:FindPartOnRayWithIgnoreList(extendedRay, {workspace.Hits, gunTip.Parent.FireLeft})
        CreateBeam(position, gunTip,Direction, speed)

That is server side of the code, now to answer the question @REALTimothy0812 had

The client invokes the server with this;

local mousepos = mouse.Hit
fireGun:InvokeServer("Gun Name", gun, fireCount, mousepos)

If theres anything else you need to help me towards a solution, let me know. All help is much appreciated.

1 Like

Still isn’t working, I tried doing this though;

return Ray.new(gunTip.Position, Auto(mousehit.p - gunTip.CFrame.p, math.rad(5)) * (range))

I used a different raycasting script, where it doesn’t follow the mouse, and it worked fine, and shot out of the barrel, so now I am sure raycasting is the issue, and I still don’t know how to make a startpos ( the barrel ) for the raycasting.

Where is extendedRay I don’t see it.

I hate to be that guy, but have you checked in game if the gun tip is nicely placed?

Also, Why are you doing: *CFrame.Angles(0, 0, 0, 2*math.pi)? If its a ray angles don’t matter, right?