How to raycast forward without using Players Mouse?

I am trying to create Tank that will shoot straight not to mouse position, how I can raycast from tank output part to the straight point, that’s gun pointing at?

My question may be confusing, but basically i want to make shooting tank, using raycast without using mouse, just to shoot straight… Any ideas?

Here is my example how I want to shoot straight (with 200 distance limit)

1 Like

This resource on raycasting seems like it’d be pretty useful for your situation here. Just make the ray straight using LookVector instead of the mouse position

1 Like

you can use
workspace:FindPartOnRayWithIgnoreList
( Ray ray , Objects ignoreDescendantsTable , bool terrainCellsAreCubes , bool ignoreWater )

With a ray made like this:

Ray.new(origin, direction*distance)

origin would be the barrel position, and direction would be a unitvector, such as barrel.CFrame.lookVector

1 Like

So Ray.new(script.Parent.Output.Position, script.Parent.CFrame.LookVector*300) ?

1 Like

you can put 2 attachments in the tank gun, one located at the middle of the gun and another located at the tip, then use the positions of the attachments to calculate the direction.
Maybe something like…

Ray.new(Middle_Attachment.WorldPosition, (Middle_Attachment.WorldPosition - Tip_Attachment.WorldPosition).Unit * 300)
	local ray = Ray.new(Ray.new(OutputPart.Position, OutputPart.CFrame.LookVector * 300))
	local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
	
	local beam = Instance.new("Part", workspace)
	beam.BrickColor = BrickColor.new("Bright red")
	beam.FormFactor = "Custom"
	beam.Material = "Neon"
	beam.Transparency = 0.25
	beam.Anchored = true
	beam.Locked = true
	beam.CanCollide = false
	
	local distance = (OutputPart.CFrame.p - position).magnitude
	beam.Size = Vector3.new(0.3, 0.3, distance)
	beam.CFrame = CFrame.new(OutputPart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

I tried similar thing, but doesn’t work, nothing happens.

This need some fiddling with, but it should help.
demo.rbxl (16.5 KB)

1 Like

I also just finished a little test. Not sure if it’s completely helpful but it’s cool to look at I guess. I’ve got to go, I’ll be back in a few hours hopefully to help some more! Good luck.
raycast_demo.rbxl (16.4 KB)
quick add: this was accomplished using @johnnygadget’s idea, very clever!

2 Likes

Thank you for your hard work afford, it worked and helped, Thank you again!

1 Like