#I'm having issues with Raycasting

1 messages · Page 1 of 1 (latest)

shy lion
#

I'm making a gun that tells me the Instance of the thing hit, but it seems like it only works sometimes ? idk if it's because of the way I've attached the attachment "rayorigin" or something else, ill attach the scripts and some pictures bellow. any help would be great.

#
local gun = script.Parent.Parent
local GetMousePosition = game.ReplicatedStorage.GetMousePosition

gun.Activated:Connect(function()
    local player = game.Players:FindFirstChild(gun.Parent.Name)
    local RayOrigin = gun.Handle.RayOrigin.Position
    
    local MousePosition = GetMousePosition:InvokeClient(player)
    
    local DirectionToFire = (MousePosition - RayOrigin).Unit * 500
    
    local raycastResult = workspace:Raycast(RayOrigin, DirectionToFire)
    
    if raycastResult then
        print(raycastResult.Instance)
    end
end)
#
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local getMouseRemote = ReplicatedStorage.GetMousePosition

ReplicatedStorage.GetMousePosition.OnClientInvoke = function()
    return game.Players.LocalPlayer:GetMouse().Hit.Position
end
#

the first one is for the gun and the second is a local script that get the mouse position

polar belfryBOT
#

studio** You are now Level 3! **studio

shy lion
#

My issue is that I think the raycastorigin is still on the ground and not sticking to the gun

#

even tho it is parent to the handle

sullen mortar
#

I think you can add more accuracy for your ray

shy lion
#

how ?

shy lion
#

like the multiplying thing ?

sullen mortar
#

The direction to fire

#

Maybe if you add a lil bit of accuracy it might work better

#

I have a gun script I’ll give you a line or two later

shy lion
#

@sullen mortar

sullen mortar
#

?

shy lion
#

How do I make the accuracy better 😅

sullen mortar
#

Gonna give the code later

#

Theres a line i don’t remember

#

Ill check when ill be using my pc in 15 minutes

shy lion
#

okay, thanks a lot

floral zodiac
#

Yo! Idk if it more accurate but i think a client replication would be better

#

Whats the main issue tho? The bullet is going to the wrong direction?

#

Oh forget about it

shy lion
#

Maybe, but also what about the way the attachment is on tha handle, i added it in the workspace, but when i click it in the game it shows it as the place in the workspace, not the tip of the gun, i think thats the issue

floral zodiac
#

I read everything now

shy lion
#

But im not certain

floral zodiac
#

Can send a video of it?

polar belfryBOT
#

studio** You are now Level 1! **studio

shy lion
#

Sure, one sec

#

wtf

#

whys it work now

#

what

#

I am so sorry

#

it's working now

floral zodiac
#

My phone boutta be dead in some minutes. As soon as i get home ill try to help u asap

floral zodiac
#

First, check if the attachment0 is set to the handle and the attachment1 to the other part u desire to attach

sullen mortar
#

@shy lion

sullen mortar
#

Sorry for the double ping, wasn’t very smart of me

shy lion
#

I've never used that

#

like

#

i put the tool in the workspace, I added a Ray origin to the handle and then moved it to the Starter pack

floral zodiac
#

Can u show the attachment properties?

sullen mortar
shy lion
#

OHHHH

floral zodiac
#

It worked?

sullen mortar
#

Yo

#

Im gonna give u the line

#

I made the system a few months ago

#

The Tool.Activated Event is in the client side script

#
local function onActivated()
  local ray = Camera:ScreenPointToRay(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2 - GuiService:GetGuiInset().Y)
    if ray then
     local InvokeSuccess, errormessage = pcall(function() SendRayToServer:InvokeServer(ray) end)
      if not InvokeSuccess then warn("Invoke error: "..errormessage) end
     end
end
#

Might give a more accuracy

#

@shy lion

shy lion
#

okay thankyou