I need help with raycasting

i need help with this it keep going wrong direction everytime i looked at the wall or ground

2 Likes

It might be easier for people to help if you provided a code snippet or something lol


local RunService = game:GetService("RunService")
local bulleinfo = {}
local maxbullet = 5
local spreed = 5

script.Parent.Fire.OnServerEvent:Connect(function(player, dir, origin) 
	for i = 1, maxbullet do
		local bullet = game.ReplicatedStorage.Bullet:Clone()
		bullet.Parent = workspace.Terrain
		local x = math.random(-spreed * 100, spreed*100)/100
		local y = math.random(-spreed * 100, spreed*100)/100
		bulleinfo[bullet] = {
			origin = origin ;
			direction = (CFrame.new(origin, dir) * CFrame.Angles(math.rad(x),math.rad(y),0)).LookVector;
			speed = 300;
			lifetime = tick() + 2; 
			firedby = player.Character;
		}
	end
end)

RunService.Heartbeat:Connect(function(dt)
	for bullet, data in pairs(bulleinfo) do 
		local params = RaycastParams.new()
		params.RespectCanCollide = true
		params.FilterDescendantsInstances = {data.firedby}
		params.FilterType = Enum.RaycastFilterType.Exclude
		local ray = workspace:Raycast(data.origin, data.direction * data.speed * dt, params)
		if ray and ray.Instance.Name ~= "Bullet" then
			local hum = ray.Instance.Parent:FindFirstChildOfClass("Humanoid")
			if hum then
				hum:TakeDamage(10) 
			else
				local part = game.ReplicatedStorage.Impact:Clone()
				part.Parent = workspace
				part.CFrame = CFrame.new(ray.Position, ray.Position + ray.Normal) * CFrame.Angles(0,0,math.rad(math.random(0,360)))
				part.Color = Color3.new()
				local weld = Instance.new("WeldConstraint", part)
				weld.Part0 = ray.Instance
				weld.Part1 = part

				game.Debris:AddItem(part, 5)
			end
			bullet:Destroy()
			bulleinfo[bullet] = nil
		else
			data.origin += data.direction * data.speed * dt
			bullet.CFrame = CFrame.new(data.origin, data.origin + data.direction)
			
			if tick() > data.lifetime then
				bullet:Destroy()
				bulleinfo[bullet] = nil
			end
		end
	end
end)





1 Like

That’s because you’re calculating the direction and starting point on the server. Calculate them both on the client then send them to the server.

so um u meant i have to Calculate them on clients first and then send to server ?

1 Like

Yes, this includes the direction and start only. Not the bullets themselves of course.

1 Like

still the same so u meant smt else ?

1 Like

Ohh… I get it now what you mean… I thought you meant something else LOL!

Anyways, yeah for that issue you need to cast the rays a bit back from the actual point of the gun but render the bullet from the muzzle. There isn’t any other fix for it.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.