Unity3d 检测物体在不在摄像机照射范围内
作者:
Kyle_An | 来源:发表于
2017-12-19 14:02 被阅读0次using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 将脚本挂在摄像机观察的物体上 物体必须带有Render
///方法在Update调用了,实时监测的
/// </summary>
public class PlayerView : MonoBehaviour {
bool isRendering;
float curtTime = 0f;
float lastTime = 0f;
void OnWillRenderObject()
{
curtTime = Time.time;
}
public bool IsInView(Vector3 worldPos)
{
Transform camTransform = Camera.main.transform;
Vector2 viewPos = Camera.main.WorldToViewportPoint(worldPos);
Vector3 dir = (worldPos - camTransform.position).normalized;
float dot = Vector3.Dot(camTransform.forward, dir); //判断物体是否在相机前面
if (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1)
return true;
else
return false;
}
void Update()
{
Vector2 vec2 = Camera.main.WorldToScreenPoint(this.gameObject.transform.position);
if (IsInView(transform.position))
{
Debug.Log("目前本物体在摄像机范围内");
}
else
{
Debug.Log("目前本物体不在摄像机范围内");
}
}
}
本文标题:Unity3d 检测物体在不在摄像机照射范围内
本文链接:https://www.haomeiwen.com/subject/kjibwxtx.html
网友评论