Raycast not going into the correct direction

External Media

As you can see I am trying to hit the npc but the ray is going in a different direction

My script

local function performRaycast(character, range)
	local startPosition = character.PrimaryPart.Position
	local endPosition = startPosition + character.PrimaryPart.CFrame.LookVector * range

	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = {character}

	local result = workspace:Raycast(startPosition, endPosition, raycastParams)

	if result then
		local distance = (startPosition - result.Position).Magnitude
		local p = Instance.new("Part")
		p.Anchored = true
		p.CanCollide = false
		p.Size = Vector3.new(0.1, 0.1, distance)
		p.CFrame = CFrame.lookAt(startPosition, result.Position)*CFrame.new(0, 0, -distance/2)
		p.Parent = workspace
	end
	return result
end


local function applyDamageToNPC(hitCharacter, damage)
	local humanoid = hitCharacter:FindFirstChild("Humanoid")
	if humanoid and not hitCharacter:FindFirstChild("Player") then
		print("Applying damage to NPC")
		humanoid:TakeDamage(damage)
		return true
	else
		print("Humanoid not found or hitCharacter is a player")
	end
	return false
end



local function playHitReaction(hitCharacter, hitReactionAnimationId)
	local humanoid = hitCharacter:FindFirstChild("Humanoid")
	if not hitCharacter or not humanoid then return end

	local animation = Instance.new("Animation", hitCharacter)
	animation.AnimationId = "rbxassetid://" .. hitReactionAnimationId
	local animationTrack = humanoid:LoadAnimation(animation)
	animationTrack.Priority = Enum.AnimationPriority.Action
	animationTrack:Play()
end

LuffiModule.Punch = function(player, punchIndex, punchRange, punchDamage)
	local punchAnimationId = LuffiModule.PunchAnimations[punchIndex]
	playPunchAnimation(player, punchAnimationId)

	wait(0.1) -- You can adjust this delay for when the raycast should be performed during the animation

	local character = player.Character
	local result = performRaycast(character, punchRange)


	if result and result.Instance.Parent:IsA("Model") then
		local hitCharacter = result.Instance.Parent
		print("Raycast hit:", hitCharacter.Name)

		local npcHit = applyDamageToNPC(hitCharacter, punchDamage)
		if npcHit then
			print("Damage applied to:", hitCharacter.Name)
			local hitReactionAnimationId = LuffiModule.HitReactionAnimations[punchIndex]
			playHitReaction(hitCharacter, hitReactionAnimationId)
		end
	else
		print("Raycast did not hit a model")
	end


end

To Get the Direction, you subtract the Vector, not add it.

Which it should be something like this:

local Direction = (startPosition - character.PrimaryPart.Position).Unit * range
1 Like

now there is no result when I change the code

For the raycast direction. You should just do

character.PrimaryPart.CFrame.LookVector * range
1 Like

Ah, I see what I did, The code I have is basically subtracting itself, which will result in it being 0, and 0 times the range is 0

You could, But it would only cast the ray in a foward direction

I understand, but this is what OP is trying to do.

1 Like

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