这个代码可以用于检测玩家是否移动了,或者其他行为(比如,跳跃,摔倒,等等)。
直接贴代码:
local avatar = script.Parent
function GetVector3Dot(v1, v2) --计算向量
return v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z
end
function aradius(ros1,ros2,pos1,pos2,r,angle) --计算两物体角度,并且返回玩家是否在NPC的范围内
--r是扇形半径,angle是角度
local t0 = ros1.Y
local t = t0*math.pi/180 --玩家当前朝向于z轴正向夹角
local v1 = Vector3.new(math.sin(t),0,math.cos(t))*(-1) --玩家本身单位向量
local pos = pos1 - pos2
local poss = pos/math.sqrt(pos.X*pos.X+pos.Y*pos.Y+pos.Z*pos.Z)
local possuc = Vector3.new(poss.X,0,poss.Z)
local cos = GetVector3Dot(v1, possuc)
local j = math.acos(cos)
local jd = j*180/math.pi
local dis = (pos).magnitude
return jd < angle/2 and dis<r
end
while wait() do
for k,player in pairs(game.Players:GetChildren()) do
local success,err = pcall(function()
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local bool = aradius(avatar.HumanoidRootPart.Orientation,player.Character.HumanoidRootPart.Orientation,avatar.HumanoidRootPart.Position,player.Character.HumanoidRootPart.Position,999999,175)
local GetState_T = player.Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Running and player.Character.Humanoid:GetState() ~= Enum.HumanoidStateType.RunningNoPhysics
local MoveDirection_T = player.Character.Humanoid.MoveDirection
if player.Character.Humanoid.Health > 0 and (MoveDirection_T ~= Vector3.new(0,0,0) or not GetState_T == false) then
print("触发")
end
end
end)
if not success then
warn(err)
end
end
end