So, I’m making a character-like fighting game, and when I tried raycasting for the first time – this is what I got.
Basically when I shoot, the minigun shoots a bullet (still in progress). However, it shoots behind the player instead of what I’m trying to achieve, which is in front of the player. Also, I have no idea how to increase the range of the raycast, I’ve heard its from the direction, but when I tried changing it (origin - direction), (origin + direction), it still ended up the same way. The one in the photo below is origin - direction.
Also, its noted that when the player shoots, a block in front of the player spawns about 30 studs in front of the player that way the bullet can show without wasting ammo for the player. (which is why theres so many grey boxes behind me)
Code:
repeat wait(0.1)
if ranout == false then
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {game.Workspace:FindFirstChild(player.Name), game.Workspace:FindFirstChild(player.Name).meshes:GetChildren()}
local max = game.Workspace:WaitForChild("MINIGUNNERmax"):Clone()
max.Name = player.Name .. " max"
max.Parent = workspace
local dist = -30
max:SetPrimaryPartCFrame(CFrame.new(Character.meshes.Meshes.CFrame.Position+(dist*Character.meshes.Meshes.CFrame.LookVector),Character.meshes.Meshes.Position)*CFrame.Angles(0, math.rad(0), 0))
local origin = game.Workspace:WaitForChild(player.Name).meshes.shootarea.Position
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {
game.Workspace:FindFirstChild(player.Name),
game.Workspace:FindFirstChild(player.Name).meshes:GetChildren(),
game.Workspace.INGREDIENTSPART:GetChildren()
}
params.IgnoreWater = true
local direction = max.Part.Position
local ray = workspace:Raycast(origin, origin-direction, params)
local shootarea = game.Workspace:FindFirstChild(player.Name).meshes.shootarea
if ray then
print("hi!")
print(ray.Instance.Name)
local Dis = (ray.Position - shootarea.Position).magnitude
local Dir = (ray.Position - shootarea.Position)
local MiddlePoint = shootarea.Position + Dir/2
local bullet = Instance.new("Part",workspace)
bullet.Name = "minigunBullet" .. " player.Name"
bullet.CFrame = CFrame.new(MiddlePoint,ray.Position)
bullet.Size = Vector3.new(0.5,0.5,Dis)
bullet.Material = Enum.Material.Neon
bullet.CanCollide = false
bullet.Anchored = true
coroutine.resume(coroutine.create(function()
wait(2)
local boolvalue = false
for i = 0, 1, 0.05 do
wait(0.03)
bullet.Transparency = i
if bullet.Transparency >= 1 then
boolvalue = true
end
end
bullet:Destroy()
end))
end
end
until playerMouse.Value == false
(also, sorry for the amount of spaces i put between each line, it helps me organize the code a bit more when i read it.)
ranout is a bool that detects if the player is out of bullets,
this is a portion of what is actually in the script (because the other parts are just for things like walkspeed change, status effects, etc)
there are no errors in the output.
this is a module script. (player is passed from module scripts. (i use module trees for the abilities.))
MINIGUNNERmax is the grey block (which is a model) that spawns in front of the player.