本文主要是介绍Unity---视窗坐标转化和判断物体是否出了屏幕边界,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
视窗坐标转世界坐标和屏幕坐标,判断物体是否还在边界内,和判断从那边出界,先看效果:
这里是直接拖动物体改变坐标来检测,从图中的打印可以清楚看到效果,旋转摄像机视角是同样的效果,下面是代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Test : MonoBehaviour
{public Transform player;public float myWidth;public float myHeight;void Start(){Debug.Log("Screen.width" + Screen.width);Debug.Log("Screen.height" + Screen.height);}void Update(){CornerPos();}void CornerPos(){/// <summary>/// 视窗坐标转世界坐标。视窗坐标是以视窗左下角(0,0)坐标为起始点,长宽为1./// </summary>Vector3 LeftUpWorldoCornerPos = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, -Camera.main.transform.position.z));//视窗左上角转为世界坐标Vector3 LeftDownWorldCornerPos = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, -Camera.main.transform.position.z));//视窗左下角转为世界坐标Vector3 RightUpWorldCornerPos = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, -Camera.main.transform.position.z));//视窗右上角转为世界坐标Vector3 RightDownWorldCornerPos = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, -Camera.main.transform.position.z));//视窗右下角转为世界坐标/// <summary>/// 视窗坐标转世界屏幕,可以手动得到其屏幕长宽/// </summary>Vector3 LeftUpScreenCornerPos = Camera.main.ViewportToScreenPoint(new Vector3(0, 1, -Camera.main.transform.position.z));//视窗左上角转为屏幕坐标Vector3 LeftDownScreenCornerPos = Camera.main.ViewportToScreenPoint(new Vector3(0, 0, -Camera.main.transform.position.z));//视窗左下角转为屏幕坐标Vector3 RightUpScreenCornerPos = Camera.main.ViewportToScreenPoint(new Vector3(1, 1, -Camera.main.transform.position.z));//视窗右上角转为屏幕坐标Vector3 RightDownScreenCornerPos = Camera.main.ViewportToScreenPoint(new Vector3(1, 0, -Camera.main.transform.position.z));//视窗右下角转屏幕界坐标myWidth = Vector3.Magnitude(LeftUpScreenCornerPos - RightUpScreenCornerPos);myHeight = Vector3.Magnitude(LeftUpScreenCornerPos - LeftDownScreenCornerPos);/// <summary>/// 视窗坐标转世界坐标。得到四个视窗中心点的世界坐标,与物体判断是否到达边界./// </summary>Vector3 upMiddlePos = Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 1f, -Camera.main.transform.position.z)); //视窗上方中心转为世界坐标Vector3 downMiddlePos = Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0, -Camera.main.transform.position.z));//视窗下方中心转为世界坐标Vector3 leftMiddlePos = Camera.main.ViewportToWorldPoint(new Vector3(0, 0.5f, -Camera.main.transform.position.z));//视窗左边中心转为世界坐标Vector3 rightMiddlePos = Camera.main.ViewportToWorldPoint(new Vector3(1, 0.5f, -Camera.main.transform.position.z));//视窗右边中心转为世界坐标bool upOut = upMiddlePos.y - player.position.y <= 0 ? true : false; //与上方屏幕边缘碰到否bool downOut = downMiddlePos.y - player.position.y >= 0 ? true : false;//与下方屏幕边缘碰到否bool leftOut = leftMiddlePos.x - player.position.x >= 0 ? true : false;//与左边屏幕边缘碰到否bool rightOut = rightMiddlePos.x - player.position.x <= 0 ? true : false;//与右边屏幕边缘碰到否Debug.Log("upOut : " + upOut);Debug.Log("downOut : " + downOut);Debug.Log("leftOut : " + leftOut);Debug.Log("rightOut : " + rightOut);}
}
注释很清楚了,这里的Player就是Cube.
欢迎加群:4364930讨论。
这篇关于Unity---视窗坐标转化和判断物体是否出了屏幕边界的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!