Weapon Raycast Goes in Random Direction?

Hello, I have recently been encountering an issue whilst trying to make my sword raycast script. The parts that are meant to visualize the raycast look fine and accurate, but what the raycast actually hits is incorrect and I’m not sure why.


In the video example, the dots are representing the points where the raycast is starting and ending, and a line is drawn inbetween them. The parts that you can see turning pink, however, are what the raycast is really hitting.
I’m not really sure what I’m doing wrong, so here’s a code snippet that does the raycast.

local raycastResult = workspace:Raycast(Attachment1.WorldPosition, prevPos, raycastParams)
		local part = Instance.new("Part")
		part.Parent = workspace
		part.Anchored = true
		part.Position = Attachment1.WorldPosition
		part.Size = Vector3.new(0.5, 0.5, 0.5)
		part.Name = ("start")
		part.Color = Color3.new(0, 1, 0)
		local part2 = Instance.new("Part")
		part2.Parent = workspace
		part2.Anchored = true
		part2.Position = prevPos
		part2.Size = Vector3.new(0.5, 0.5, 0.5)
		part2.Name = ("end")
		part.Color = Color3.new(1, 0, 0)
		-- Create the visualizer part
		local visualizer = Instance.new("Part")
		visualizer.Anchored = true
		visualizer.CanCollide = false
		visualizer.BrickColor = BrickColor.new("Institutional white")
		visualizer.Transparency = 0.75
		visualizer.Parent = workspace.Rays
		local raycastDistance = (prevPos - Attachment1.WorldPosition).magnitude
		visualizer.Size = Vector3.new(0.3, 0.3, raycastDistance)
		visualizer.CFrame = CFrame.new(prevPos, Attachment1.WorldPosition)
		game:GetService("Debris"):AddItem(visualizer, 1)
		if raycastResult then
			local hitPart, hitPosition = unpack({raycastResult.Instance, raycastResult.Position})
			print(hitPart)
			hitPart.Color = Color3.new(1, 0.596078, 0.772549)
			local hashum = hitPart.Parent:FindFirstChild("Humanoid") or hitPart.Parent.Parent:FindFirstChild("Humanoid") or hitPart.Parent.Parent.Parent:FindFirstChild("Humanoid")
			if hashum then
				local enemyHum = hashum
				visualizer.BrickColor = BrickColor.new("Light red")
				visualizer.Transparency = 0.5
				if enemyHum.Health - damage <= 0 and math.random(0, 2) == 0 then
					Handle.Kill:Play()
					local neck = enemyHum.Parent.Torso:FindFirstChild("Neck")
					if neck then
						enemyHum.Parent.Torso.Neck:Destroy()
						enemyHum.Parent.Head.Position = Vector3.new(enemyHum.Parent.Head.Position.X, enemyHum.Parent.Head.Position.Y + 0.5, enemyHum.Parent.Head.Position.Z)
					end
				end
				Handle.HitPlayer:Play()
				enemyHum:TakeDamage(damage)
				TagHumanoid(enemyHum, player)
			else
				Handle.Hit:Play()
				visualizer.BrickColor = BrickColor.new("Dark Baby blue")
				visualizer.Transparency = 0.5
			end
			return true
		end
	end
	prevPos = Attachment1.WorldPosition

Any help fixing my code would be much appreciated!

Could be wrong, Im pretty sure you have to subtract LastPos and Attachment’s WorldPosition

1 Like

Could you elaborate on where to do this please?

Looks like the raycast is shooting straight from the tip of the sword during the animation.
That’s why the first swing has the red dot behind the player, but at the end of the animation the sword is pointing at the purple block.
Shouldn’t it be pointing from the blade edge of the swinging sword to see where it’s contacting?

When you start to swing, prevPos is set to the Attachment1.WorldPosition. Attachment1 is an Attachment that is in the center of the sword’s blade. When the swing progresses, it is supposed to raycast from prevPos to the new Attachment1.WorldPosition and then set prevPos to Attachment1.WorldPosition. the return false causes the swing to stop when something is hit. Currently, it seems like the raycast is not matching up to what is shown by the visualizer part, even though they should be the same.

Would rotating your Attachment cause it to point the correct direction?

No, it’s an Attachment. The Attachment1.WorldPosition is returning a Vector3 in which rotation is not a factor, nor would matter.

Basically for the ray direction, i mean this:

(LastPos - Attachment.WorldPosition)

Could be wrong tho

I’m nowhere near a pro at this, but could you use the sword blade’s LookVector, RightVector or UpVector to get which way the edge is swinging then if the raycast returns a 0 distance stop the animation and return the Part the raycast is pointing at?

Unfortunately this doesn’t work.

I could technically do that, but it causes blank spots to be left when the sword is swung. I want to use a system like the intended current one for precision.

bump

I figured this out myself. I was using the second argument improperly.

1 Like

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