Wacky Raydetect results

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make it so that the fox will always look for players in the level range that it will attack (The level is not causing the problem).
  2. What is the issue? Include screenshots / videos if possible!
    I thought the problem was that it was clipping through players, but no. It can detect players but its direction is completely off. Is there ANY explanation to this at all?

    I have tried rotating it around but that still did nothing. I added params to a part that is really big and follows the player around, but that did nothing.
task.wait(2)
local function RaycastFunction()
for i, v in pairs(game.Players:GetPlayers()) do
	if v.Character:WaitForChild("PlayerStats").EXP.Level.Value <= 10 then
		local Params = RaycastParams.new(game.Workspace.BackgroundAuras.Globals)
		local RayCast = game.Workspace:Raycast(script.Parent.RayCastPart.Position, v.Character.HumanoidRootPart.Position, Params)
 		task.wait(0.33)
		local RaySuccess = true
		if RayCast == nil then
			RaySuccess = false
			print("rayfailure")
		end
		task.wait(0.33)
		if RaySuccess == true then
			print(RayCast.Instance.Parent)
		end
	end
end
task.wait(0.34)
RaycastFunction()
end
RaycastFunction()```

try this:

local Difference = (script.Parent.RayCastPart.Position - v.Character.HumanoidRootPart.Position)
			
local RayCast = game.Workspace:Raycast(script.Parent.RayCastPart.Position, Difference.Unit * Difference.Magnitude, Params)

I wouldn’t use Raycast. I would use Region3 and RunService, so something like this:

--// Services
local RunService = game:GetService("RunService");

--// Variable
local Pet = -- Your Pet's model.
local Radius = 20; -- Max radious to look at player.
local RotationVelocity = 0.2; -- Percentage (0 - 1)

--// Coding
local function lookAtPlayer(PlayerPos)

   local direction = (playerPos - Pet.PrimaryPart.Position).Unit;

   local angle = CFrame.new(pet.PrimaryPart.Position, Pet.PrimaryPart.Position + direction);

   Pet:SetPrimaryPartCFrame(Pet.PrimaryPart.CFrame:Lerp(angle, RotationVelocity))

end

local function findNearestPlayer(Parts)
   -- Check distances with Region3 parts.
end

RunService.Heartbeat:Connect(function()
    local region = Region3.new( ... )-- create your Region
    local PartsInRegion = workspace:FindPartsInRegion3(region, Pet, math.huge)

    local ObjetivePlayer = findNearestPlayer(PartsInRegion )
    if ObjetivePlayer then
        local PlayerPos = ObjetivePlayer .Character.PrimaryPart.Position
        lookAtPlayer(PlayerPos ) -- Pet look at nearest player
        
        -- your code here (...)

    end
end)

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