You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
(sorry for my bad english) I am trying to create a firearm for a 2D shooter game. I will explain it with the picture below: the gun only needs to be able to aim up and down (or in the Z axis, the blue one) and I’m also trying to make it so that you don’t have to click on the player directly, instead that you can fire for example in the image below, fire at the target and still hit the dummy behind.
-
What is the issue? Include screenshots / videos if possible!
I cannot find a way to create this gun. I think it needs to use the mouse position on the screen, but I am really new at scripting and can’t make something that aims only based on a 2D mouse position.
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried to use guns that made you click on the player to damage them, but then you would be able to shoot through walls.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
I don’t know which category I should put that in, so I chose scripting support. if there is another category I should have used, please tell me which one.
Using the LookVector of the characters humanoid, and the Hit Position of the mouse, you can determine the direction of a raycast.
The Y Axis is up and down, and the X and Z are forward, backward, left and right.
By using mouse.Hit.Position
, you can get a Vector3 world position of the mouse, you can then use that positions Y to determine the angle, then you just take the X and Z of the HumanoidRootParts LookVector.
(In a local script)
local player = game.Players.LocalPlayer
local Character = player.Character
local mouse = player:GetMouse()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
-- (whenever the mouse is clicked, use this to get he raycast direction)
local Direction = Vector3.new(HumanoidRootPart.CFrame.LookVector.X, mouse.Hit.Position.Y, HumanoidRootPart.CFrame.LookVector.Z) * 100
1 Like
That script by itself wont work.
You should look at how to RayCast.
thanks! but how do I limit the raycast range?
Changing the Multiplier in the math to a different number will affect the range, or you can do the full raycast, and when it hits an object, it will then check if the hit position is a certain distance away from the player.
You can do this easily with something like this:
if (HumanoidRootPart.Position - Raycast.Hit.Position).Magnitude <= MaxDistance then
-- do stuff here
end
the .Magnitude
will get the distance (in studs, i think) that position A is from position B.
1 Like
I tried to use the raycast script and changing the variables with the ones in the direction script, but it doesn’t do anything.
local player = game.Players.LocalPlayer
local Character = player.Character
local mouse = player:GetMouse()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
-- (whenever the mouse is clicked, use this to get he raycast direction)
local Direction = Vector3.new(HumanoidRootPart.CFrame.LookVector.X, mouse.Hit.Position.Y, HumanoidRootPart.CFrame.LookVector.Z) * 100
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {HumanoidRootPart.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(HumanoidRootPart, Direction, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
-- Check if the part resides in a folder, that it's fully visible, and not locked
if hitPart.Parent == workspace.Tiles and hitPart.Transparency == 0 and not hitPart.Locked then
print ("hit")
end
end
The Raycasting in that script runs the exact moment the HumanoidRootPart loads into the game.
If you are using tools for the weapons (which i highly dislike) you should get the direction and then do the raycasting when the tool is activated.
Again, this would be in a local script, inside the tool.
local player = game.Players.LocalPlayer
local Character = player.Character
local mouse = player:GetMouse()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local tool = script.Parent -- (this is for if the script is inside a tool)
tool.Activated:Connect(function()
local Direction = Vector3.new(HumanoidRootPart.CFrame.LookVector.X, mouse.Hit.Position.Y, HumanoidRootPart.CFrame.LookVector.Z) * 100
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {HumanoidRootPart.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(HumanoidRootPart, Direction, raycastParams)
if raycastResult and raycastResult.Instance and raycastResult.Position then
local hitPart = raycastResult.Instance
-- Check if the part resides in a folder, that it's fully visible, and not locked
if hitPart.Parent == workspace.Tiles and hitPart.Transparency == 0 and not hitPart.Locked then
print ("hit")
end
end
end
I should also mention that the Raycasting should like this:
7 Likes