Unity 获得相机在某一平面上可视范围的四个角的世界坐标

射线方法(纯计算,无物理)

var leftButtom = new Vector3(0, 0, 12);
var leftTop = new Vector3(0, Screen.height, 12);
var rightTop = new Vector3(Screen.width, Screen.height, 12);
var rightButtom = new Vector3(Screen.width, 0, 12);

var plane = new Plane(PlaneGameObject.transform.up, PlaneGameObject.transform.position);

var ray_leftButtom = m_camera.ScreenPointToRay(leftButtom);
var ray_leftTop = m_camera.ScreenPointToRay(leftTop);
var ray_rightTop = m_camera.ScreenPointToRay(rightTop);
var ray_rightButtom = m_camera.ScreenPointToRay(rightButtom);

Debug.DrawRay(ray_leftButtom.origin, ray_leftButtom.direction, Color.white, 1f);
Debug.DrawRay(ray_leftTop.origin, ray_leftTop.direction, Color.white, 1f);
Debug.DrawRay(ray_rightTop.origin, ray_rightTop.direction, Color.white, 1f);
Debug.DrawRay(ray_rightButtom.origin, ray_rightButtom.direction, Color.white, 1f);

float enter = 0;

if (plane.Raycast(ray_leftButtom, out enter))
{
    Cubes[0].transform.position = ray_leftButtom.GetPoint(enter);
}

if (plane.Raycast(ray_leftTop, out enter))
{
    Cubes[1].transform.position = ray_leftTop.GetPoint(enter);
}

if (plane.Raycast(ray_rightTop, out enter))
{
    Cubes[2].transform.position = ray_rightTop.GetPoint(enter);
}

if (plane.Raycast(ray_rightButtom, out enter))
{
    Cubes[3].transform.position = ray_rightButtom.GetPoint(enter);
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值