Ok, I think I am doing what you and another user have told me, but it is still work.
I am casting two rays, first one with data from :ViewportPointToRay (I also tried with :ScreenPointToRay) and the second ray one based on the first one, like this:
local MouseLocation = UserInputService:GetMouseLocation()
local ViewRay = workspace.Camera:ScreenPointToRay(MouseLocation.X, MouseLocation.Y)
local mousePos = Ray.new(ViewRay.Origin, ViewRay.Direction * 300)
local ray = workspace:Raycast(MuzzlePosition, mousePos.Direction * 300)
For the âlocal ray = workspace:Raycast(MuzzlePosition, mousePos.Direction * 300)â I also tried to do âmouse.Pos.Origin - MuzzlePositionâ (mouse.Pos.Position does not exist, had to use Origin) as you told me here but it didnât work.
Where exacly is my code wrong? By the way, the rest of the code stayed unchanged, just like how I posted it in the beginning.
FindPartOnRay is deprecated. Here is my code but it doesnât work, where exactly is the problem?
local MouseLocation = UserInputService:GetMouseLocation()
local ViewRay = workspace.Camera:ViewportPointToRay(MouseLocation.X, MouseLocation.Y)
local mousePos = Ray.new(ViewRay.Origin, ViewRay.Direction * 300)
local ray = workspace:Raycast(MuzzlePosition, mousePos.Direction * 300)
Here the only difference between the code in the video and mine is this line which I replaced since the one in the video is deprecated:
local ray = workspace:Raycast(MuzzlePosition, mousePos.Direction * 300)
Looks to me like you want the ray to aim towards wherever you mouse lands in world space.
You can achieve this by using the GetMouse function to get the mouse object, and then read the hit property.
This can be done using the following code:
local Client = game:GetService("Players").LocalPlayer -- Get the player object
local Mouse = Client:GetMouse() -- Get the mouse object
local MousePosition = Mouse.hit.Position -- hit is a CFrame, we only want the position
-- When then find the difference between our Raycast' origin and this point to get the direction of our raycast
local Direction = (MousePosition - MuzzlePosition)
In this code sample provided Direction would replace ViewRay.Direction in your Raycast.
You currently are not doing it in two steps, in this code, you are still making a raycast using the ViewRay and Muzzle directly
Also, btw, ViewRay and mousePos are like the same thing but with a longer direction, that you make longer a second time when raycasting
Here, ray should be
local RaycastResult = workspace:Raycast(mousePos.Origin, mousePos.Direction * 300)
if RaycastResult then
local GunRay = ray.new(MuzzlePosition, RaycastResult.Position - MuzzlePosition)
-- Here you can perform a new raycast using GunRay
end
How does it look when shooting at the floor or a much closer object. To me it looks right, its just that the bullet part doesnt extend long enough.
Sorry I didnt read the part that mentioned that it works in close positions. Still, the bullet part doesnt seem to be long enough.
local RaycastResult = workspace:Raycast(GunRay.Origin, GunRay.Direction * 1.1) -- Making the ray a bit longer than the distance, to make sure it hits
There is nothing fancy going on here, itâs just a raycast that follows GunRay
You can then use the RaycastResult to find if it hit a player or whatever your gun is supposed to shoot at
Everyhting seems to work fine now, but not when the range of the ray that I cast from the muzzle of the gun is out. Meaning this part of the code doesnât work:
if ray ~= nil then
--Some Code
else--if the ray hit maximum range
CachedBullet.CFrame = CFrame.lookAt(MuzzlePosition + (ViewRay.Direction * 300)/2, MuzzlePosition + ViewRay.Direction * 300) * CFrame.Angles(0, math.rad(90), 0)
CachedBullet.Size = Vector3.new(300, 0.15, 0.15)
end
Basically a similar result as before when I just started this post happens. I suppose the formula of the CFrame.lookAt is wrong, but I donât know where exactly. By the way, I use CFrame.Angles to rotate the part since it is a Cylinder sphere.
Here instead of using ViewRay, you should use GunRay. ViewRay is the ray of the screen (aka the camera) to the mouse, while gun ray is the ray from the gun to the mouse, and thus should be used instead
If you want the bullets to hit where you clicked, then you should cast a ray from the camera to the mouse.
function RaycastModule.rayCastToInput(origin, inputObject,passed_Params)
local Camera = workspace.CurrentCamera
local worldRay = Camera:ScreenPointToRay(inputObject.Position.X, inputObject.Position.Y)
return rayCast(origin, worldRay.Direction,passed_Params)
end