有关这个问题,大致提两种思路,其实原理也都类似
1、使用角度:首先计算角色和物品的距离,是在附近的距离范围内(至于多少算近,这个自己定义好距离),使用Vector3.Distance,如果是在附近,然后计算角色和物品的角度使用Vector3.Angle计算是否在角色的视野范围内(至于角度,也是自己觉得多少度算合适就行)
下面见代码,代码是lua写的,我就不改了,和C#类似
function GetInSightBoxes(hero)
local newBoxes = {}
local pos = hero:GetPosition()
local cameraController = hero.cameraController
local lookForward = cameraController.cameraTrans.forward
for uid, box in pairs(Boxes) do
local dist = Vector3.Distance(Vector3(box.position.x, 0, box.position.z), Vector3(pos.x, 0, pos.z))
if dist < 5 then
box.distance = dist
local toOther = Vector3(box.position.x, 0, box.position.z) - Vector3(pos.x, 0, pos.z)
local angle = Vector3.Angle(toOther, lookForward)
if angle < 45 then
newBoxes[uid] = box
end
end
end
return newBoxes
end
2、使用点乘 ,和上面类似,范围值是(-1,1)