You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
Im trying to make a flying enemy that attacks the player. Its going well so far and the only thing left is this issue. -
What is the issue? Include screenshots / videos if possible!
When loaded, the enemies meshpart sinks into the middle of the character. It should look like this:
But looks like this:
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried switching CFrame usage to :MoveTo, but it still doesnt work.
local model = script.Parent
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local speed = 32
local refreshRate = 0.000000000000001
local targetRadius = 2000
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {model}
local function findClosestPlayer()
local closestPlayer = nil
local closestDistance = math.huge
for _, player in pairs(game.Players:GetPlayers()) do
local character = player.Character
if character and character.PrimaryPart then
local distance = (character.PrimaryPart.Position - model.PrimaryPart.Position).Magnitude
if distance < closestDistance then
closestPlayer = character
closestDistance = distance
end
end
end
return closestPlayer
end
local function isPathClear(targetPosition)
local currentPosition = model.PrimaryPart.Position
local direction = (targetPosition - currentPosition).Unit
local distance = (targetPosition - currentPosition).Magnitude
local rayY = workspace:Raycast(currentPosition, Vector3.new(0, direction.Y, 0) * distance, raycastParams)
local rayZ = workspace:Raycast(currentPosition, Vector3.new(0, 0, direction.Z) * distance, raycastParams)
return not rayY and not rayZ
end
local function moveModelTo(targetPosition)
for _, part in pairs(model:GetDescendants()) do
if part:IsA("BasePart") then
local distance = (targetPosition - part.Position).Magnitude
local timeToMove = distance / speed
local tweenInfo = TweenInfo.new(timeToMove, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local tween = tweenService:Create(part, tweenInfo, {Position = targetPosition})
tween:Play()
end
end
end
local function onTouch(other)
if other and other.Parent and other.Parent:FindFirstChild("Humanoid") then
local humanoid = other.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(10)
end
end
end
local function followPlayer()
while true do
local target = findClosestPlayer()
if target and target.PrimaryPart then
local targetPosition = target.PrimaryPart.Position
if isPathClear(targetPosition) then
moveModelTo(targetPosition)
local direction = (targetPosition - model.PrimaryPart.Position).Unit
local targetRotation = CFrame.new(model.PrimaryPart.Position, targetPosition)
model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(180), 0))
end
end
wait(refreshRate)
end
end
model.PrimaryPart.Touched:Connect(onTouch)
followPlayer()