What i’m trying to do is get the part in the middle of the screen and return it via script. Local script
Just like the mouse target.
If possible please without recasting
What i’m trying to do is get the part in the middle of the screen and return it via script. Local script
Just like the mouse target.
If possible please without recasting
You would have to get a ray that’s made with Camera:ViewportPointToRay(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2)
which would give you a ray that’s exactly the middle point of your screen. Then you would have to raycast from there.
How do I implement this into a script
Also you did not even get the camera
That was an example of what you should do, not a fully fledged script.
I did never do ray costing before.
Please give me a helping hand
Could you eventually please try to do so?
You don’t have to make that many replies, this is a forum, not a chatting platform. I am going to help you implement this into a script.
You would first get the camera like so
local camera = workspace.CurrentCamera
Then you would declare raycastParams
as the RaycastParams, which is the parameters you use for the raycasting.
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {}
With the actual raycastParams you can change how the raycast behaves, such as ignoring certain parts, or only including certain parts. If you don’t need that you can skip the params for now.
Then you would get the ray that you need.
local middleRay = camera:ViewportPointToRay(camera.ViewportSize.X/2, camera.ViewportSize.Y/2)
And now you would do the actual raycasting.
local raycastResult = workspace:Raycast(middleRay.Origin, middleRay.Direction * 1000, raycastParams)
workspace:Raycast
takes three arguments: The first is the origin of the raycast, meaning where it’s gonna cast from. The second is the direction of the ray, where it’s gonna go. Note that the magnitude of the direction matters, it determines how far the ray will travel until stopping. Here the direction is multiplied by 1000 to make it able to travel a maximum of 1000 studs. The maximum you can multiply it by is 5000. Then the third argument is the RaycastParams.
After that, to get the part that it intersected with, use
local instance = raycastResult.Instance
Do note that the result of the raycast could be nil
, and you should handle that scenario accordingly.
This results in this script.
local camera = workspace.CurrentCamera
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {}
local middleRay = camera:ViewportPointToRay(camera.ViewportSize.X/2, camera.ViewportSize.Y/2)
local raycastResult = workspace:Raycast(middleRay.Origin, middleRay.Direction * 1000, raycastParams)
local instance = raycastResult.Instance
print("Hit part:", instance)
Thank you. I am in hospital right now so I will try it when I’m back.
By the way, why do you just say workspace? I always do game.workspace
local camera = game.Workspace.CurrentCamera
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {}
local middleRay = camera:ViewportPointToRay(camera.ViewportSize.X/2, camera.ViewportSize.Y/2)
local raycastResult = workspace:Raycast(middleRay.Origin, middleRay.Direction * 1000, raycastParams)
local instance = raycastResult.Instance
if Instance ~= nil then
print("Hit part:", instance)
REshoot:FireServer(instance)
end
like this?
always returns my own head… i assume it doesnt work
thanks for making that clear
workspace
is just a shortcut for the Workspace service. It’s shorter.
It would always return your head because your head is in the middle of the screen.
how to make it not do that?
i want to use it for a weapon so it would be stupid returning myself? how to exclude my character?
path to my character from the script: script.Parent.Parent
In the raycast params you need to insert the players character into the exclusion list.
Like this:
local char = player.Character
if char then
table.insert(raycastParams.FilterDescendantsInstances, char)
end