文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-496113.html
我們?cè)趫?chǎng)景中添加物體(如:Cube),他們都是以世界坐標(biāo)顯示在場(chǎng)景中的。transform.position可以獲得該位置坐標(biāo)。
2, Screen Space(屏幕坐標(biāo)):
以像素來(lái)定義的,以屏幕的左下角為(0,0)點(diǎn),右上角為(Screen.width,Screen.height),Z的位置是以相機(jī)的世界單位來(lái)衡量的。
注:鼠標(biāo)位置坐標(biāo)屬于屏幕坐標(biāo),Input.mousePosition可以獲得該位置坐標(biāo),手指觸摸屏幕也為屏幕坐標(biāo),Input.GetTouch(0).position可以獲得單個(gè)手指觸摸屏幕坐標(biāo)。
3.ViewPort Space(視口坐標(biāo)):
視口坐標(biāo)是標(biāo)準(zhǔn)的和相對(duì)于相機(jī)的。相機(jī)的左下角為(0,0)點(diǎn),右上角為(1,1)點(diǎn),Z的位置是以相機(jī)的世界單位來(lái)衡量的。
4, 繪制GUI界面的坐標(biāo)系:
這個(gè)坐標(biāo)系與屏幕坐標(biāo)系相似,不同的是該坐標(biāo)系以屏幕的左上角為(0,0)點(diǎn),右下角為(Screen.width,Screen.height)。
【四種坐標(biāo)系的轉(zhuǎn)換】
1、世界坐標(biāo)→屏幕坐標(biāo):camera.WorldToScreenPoint(transform.position);這樣可以將世界坐標(biāo)轉(zhuǎn)換為屏幕坐標(biāo)。其中camera為場(chǎng)景中的camera對(duì)象。
2、屏幕坐標(biāo)→視口坐標(biāo):camera.ScreenToViewportPoint(Input.GetTouch(0).position);這樣可以將屏幕坐標(biāo)轉(zhuǎn)換為視口坐標(biāo)。其中camera為場(chǎng)景中的camera對(duì)象。
3、視口坐標(biāo)→屏幕坐標(biāo):camera.ViewportToScreenPoint();
4、視口坐標(biāo)→世界坐標(biāo):camera.ViewportToWorldPoint();
點(diǎn)的坐標(biāo)轉(zhuǎn)換
Transform.TransformPoint(Vector3 position); 將一個(gè)坐標(biāo)點(diǎn)從相對(duì)transform的局部坐標(biāo)系轉(zhuǎn)換為全局坐標(biāo)系
Transform.InverseTransformPoint(Vector3 position); 將坐標(biāo)點(diǎn)從全局坐標(biāo)系轉(zhuǎn)換到相對(duì)transform的局部坐標(biāo)系
方向的坐標(biāo)轉(zhuǎn)換
Transform.TransformDirection(Vector3 direction); 將一個(gè)方向從局部坐標(biāo)系轉(zhuǎn)換到全局坐標(biāo)系
Transform.InverseTransformDirection(Vector3 direction); 將一個(gè)方向從全局坐標(biāo)系轉(zhuǎn)換到局部坐標(biāo)系
Transform.TransformVector(Vector3 vector); 將一個(gè)向量從局部坐標(biāo)系轉(zhuǎn)換到全局坐標(biāo)系
Transform.InverseTransformVector(Vector3 vector); 將一個(gè)向量從全局坐標(biāo)系轉(zhuǎn)換到局部坐標(biāo)系
屏幕坐標(biāo)系和全局坐標(biāo)系
Camera.ScreenToWorldPoint(Vector3 position); 將屏幕坐標(biāo)系轉(zhuǎn)換為全局坐標(biāo)系
Camera.WorldToScreenPoint(Vector3 position); 將全局坐標(biāo)轉(zhuǎn)換為屏幕坐標(biāo)
Input.mousePosition;? 獲取鼠標(biāo)在屏幕中的坐標(biāo)
例子: 屏幕坐標(biāo)轉(zhuǎn)為世界坐標(biāo)
??????? Vector3 mousePos = Input.mousePosition;
??????? Vector3 SToW = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x,mousePos.y,1));
視口坐標(biāo)(屏幕坐標(biāo)的單位化,最大值是1)和屏幕坐標(biāo)的轉(zhuǎn)換 ?
Camera.ScreenToViewportPoint(Vector3 position); 將屏幕坐標(biāo)轉(zhuǎn)為視口坐標(biāo)
Camera.ViewPortToScreenPoint(Vector3 position); 將視口坐標(biāo)轉(zhuǎn)為屏幕坐標(biāo)
世界坐標(biāo)與視口坐標(biāo) 轉(zhuǎn)換
Camera.WorldToViewportPoint(Vector3 position); 將全局坐標(biāo)轉(zhuǎn)換為視口坐標(biāo)
Camera.ViewportWorldPoint(Vector3 position); 將視口坐標(biāo)轉(zhuǎn)換為全局坐標(biāo)
[csharp] view plain copy
??? //求角度 及前后左右方位 ?
??? public void checkTargetDirForMe(Transform target) ?
??? { ?
??????? //xuqiTest:? target.position = new Vector3(3, 0, 5); ?
??????? Vector3 dir = target.position - transform.position; //位置差,方向 ?
??????? //方式1?? 點(diǎn)乘 ?
??????? //點(diǎn)積的計(jì)算方式為: a·b =| a |·| b | cos < a,b > 其中 | a | 和 | b | 表示向量的模 。 ?
??????? float dot = Vector3.Dot(transform.forward, dir.normalized);//點(diǎn)乘判斷前后:dot >0在前,<0在后
??????? float dot1 = Vector3.Dot(transform.right, dir.normalized);//點(diǎn)乘判斷左右: dot1>0在右,<0在左
??????? float angle = Mathf.Acos(Vector3.Dot(transform.forward.normalized, dir.normalized)) * Mathf.Rad2Deg;//通過(guò)點(diǎn)乘求出夾角 ?
???? ?
??????? //方式2?? 叉乘 ?
??????? //叉乘滿足右手準(zhǔn)則? 公式:模長(zhǎng)|c|=|a||b|sin?? ?
??????? Vector3 cross = Vector3.Cross(transform.forward, dir.normalized);//叉乘判斷左右:cross.y>0在左,<0在右? ?
??????? Vector3 cross1 = Vector3.Cross(transform.right, dir.normalized); //叉乘判斷前后:cross.y>0在前,<0在后? ?
??????? angle = Mathf.Asin(Vector3.Distance(Vector3.zero, Vector3.Cross(transform.forward.normalized, dir.normalized))) * Mathf.Rad2Deg; ?
???????? ?
??? } ?
// 判斷物體是否在眼前 和 背后? fangxiang>0在眼前 , <0在背后
Vector3 dir =? target.transform.position - fpsPlayer.transform.position;
fangxiang = Vector3.Dot(fpsPlayer.transform.forward, dir.normalized);
1.判斷目標(biāo)在自己的前后方位可以使用下面的方法:
?? Vector3.Dot(transform.forward, target.position)
?????? 返回值為正時(shí),目標(biāo)在自己的前方,反之在自己的后方
2.判斷目標(biāo)在機(jī)子的左右方位可以使用下面的方法:
?? Vector3.Cross(transform.forward, target.position).y
????? 返回值為正時(shí),目標(biāo)在自己的右方,反之在自己的左方
3.在這里順便解說(shuō)下關(guān)于空間向量的點(diǎn)積和叉積:
A.點(diǎn)積
? 點(diǎn)積的計(jì)算方式為:? a·b=|a|·|b|cos? 其中|a|和|b|表示向量的模, 表示兩個(gè)向量的夾角。另外在 點(diǎn)積 中,和 夾角是不分順序的。
? 所以通過(guò)點(diǎn)積,我們其實(shí)是可以計(jì)算兩個(gè)向量的夾角的。
? 另外通過(guò)點(diǎn)積的計(jì)算我們可以簡(jiǎn)單粗略的判斷當(dāng)前物體是否朝向另外一個(gè)物體: 只需要計(jì)算當(dāng)前物體的transform.forward向量與 otherObj.transform.position 的點(diǎn)積即可, 大于0則在前方,否則在后方。
B.叉積
? 叉積的定義: c =a x b? 其中a,b,c均為向量。即兩個(gè)向量的叉積得到的還是向量!??? 性質(zhì)1: c⊥a,c⊥b,即向量c垂直與向量a,b所在的平面 。??? 性質(zhì)2: 模長(zhǎng)|c|=|a||b|sin??? 性質(zhì)3: 滿足右手法則 。從這點(diǎn)我們有axb ≠ bxa,而axb = – bxa。所以我們可以使用叉積的正負(fù)值來(lái)判斷向量a,b的相對(duì)位置,即向量b是處于向量a的順時(shí)針?lè)较蜻€是逆時(shí)針?lè)较?/span>
void OnBecameVisible()
??????? {
??????????? //Debug.LogError(this.name + "攝像機(jī)視野內(nèi)");
??????????? isVisib = true;
??????? }
??????? void OnBecameInvisible()
??????? {
??????????? //Debug.LogError(this.name + "在攝像機(jī)視野外");
??????????? isVisib = false;
??????? }
private void Update()
??????? {
??????????? if (isVisib)
??????????? {
???????????????
??????????????? Vector3 cross = this.transform.InverseTransformPoint(GFGlobal.Instance.FPSPlayerObj.transform.position);
?????????????? ?
??????????????? if (cross.x > 0)
??????????????? {
??????????????????? if (_mediaPlayer)
??????????????????? {
??????????????????????? dist = Vector3.Distance(GFGlobal.Instance.FPSPlayerObj.transform.position, this.transform.position);
??????????????????????? if(dist <= maxPauseDis)
??????????????????????? {
??????????????????????????? mPlayerPlay();
??????????????????????? }else
??????????????????????? {
??????????????????????????? mPlayerPause();
??????????????????????? }
?????????????????????? ?
??????????????????????? if (dist <= minDis)
??????????????????????? {
??????????????????????????? volume = maxvol;
??????????????????????? }
??????????????????????? else if (dist > maxDis)
??????????????????????? {
??????????????????????????? volume = 0f;
??????????????????????? }
??????????????????????? else
??????????????????????? {
??????????????????????????? volume = (dist - b) / p;
??????????????????????? }
??????????????????????? _mediaPlayer.Control.SetVolume(volume);
??????????????????? }
??????????????? }
??????????????? else
??????????????? {
??????????????????? mPlayerPause();
??????????????? }
??????????? }
??????????? // 判斷物體是否在眼前 和 背后? fangxiang>0在眼前 , <0在背后
??????????? //Vector3 dir =? this.transform.position - fpsPlayer.transform.position;
??????????? //fangxiang = Vector3.Dot(fpsPlayer.transform.forward, dir.normalized);
??????? }
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-496113.html
到了這里,關(guān)于Unity坐標(biāo)系、相互轉(zhuǎn)換和相對(duì)自身的方位及角度。的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!