線上店免費(fèi)推廣的軟件廊坊seo排名霸屏
1.? 用于將世界坐標(biāo)系轉(zhuǎn)換為屏幕坐標(biāo)系?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Camer_Class_WorldTo : MonoBehaviour
{// 用于將世界坐標(biāo)系轉(zhuǎn)換為屏幕坐標(biāo)系//本腳本將完成一個(gè)案例實(shí)現(xiàn) 小球從遠(yuǎn)處過來Transform StarFather;Transform StarChild;Vector3 ScreenPoint;Vector3 ViewPoint;Camera OneCamera;void Start(){StarFather = GameObject.FindGameObjectWithTag("Earth").transform;OneCamera = Camera.main;//獲取主攝像機(jī)}// Update is called once per framevoid Update(){ScreenPoint = OneCamera.WorldToScreenPoint(StarFather.GetChild(0).transform.position);//世界坐標(biāo)轉(zhuǎn)換 屏幕坐標(biāo)StarFather.GetChild(0).transform.RotateAround(StarFather.transform.position, Vector3.up, 50 * Time.deltaTime);ClickDestory();//實(shí)現(xiàn)小球轉(zhuǎn)后的屏幕坐標(biāo)系和鼠標(biāo)點(diǎn)擊的坐標(biāo)匹配然后刪除該物體ViewPoint = OneCamera.WorldToViewportPoint(StarFather.GetChild(0).transform.position);//世界坐標(biāo)轉(zhuǎn)換 攝像機(jī)視口坐標(biāo)}private void OnGUI(){GUILayout.BeginArea(new Rect(50, 20, 500, 300));GUILayout.Label("小球的原始坐標(biāo):" + (StarFather.GetChild(0).transform.position));GUILayout.Label("世界坐標(biāo)轉(zhuǎn)換 屏幕坐標(biāo)后" + ScreenPoint);GUILayout.Label("鼠標(biāo)屏幕坐標(biāo)" + Input.mousePosition);GUILayout.Label(("世界坐標(biāo)轉(zhuǎn)換 視口坐標(biāo)后" + ViewPoint));GUILayout.EndArea();}void ClickDestory(){Vector2 TempTurnScreen = new Vector2(ScreenPoint.x, ScreenPoint.y);Vector2 TempMousePoint = new Vector2(Input.mousePosition.x, (Input.mousePosition.y));Vector3 Lerp = TempTurnScreen - TempMousePoint;if (Lerp.magnitude < 20 & Lerp.magnitude > 0){Debug.Log("鼠標(biāo)碰撞物體成功!!!!!!!!!!!!!!!!!");if (Input.GetMouseButtonDown(0)){Destroy(StarFather.GetChild(0).gameObject);}Debug.Log("死亡位置!!!!" + ScreenPoint);}}}
2.?屏幕坐標(biāo)轉(zhuǎn)化為視口坐標(biāo) Camera.ScreenToViewportPoint和?屏幕轉(zhuǎn)世界坐標(biāo) ? ?
本案例實(shí)現(xiàn) 屏幕鼠標(biāo)移動(dòng)轉(zhuǎn)換為視口和屏幕轉(zhuǎn)世界坐標(biāo)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Camera_Class : MonoBehaviour
{// 用于測試Camera 類 坐標(biāo)系轉(zhuǎn)換//1. 屏幕坐標(biāo)轉(zhuǎn)化為視口坐標(biāo) Camera.ScreenToViewportPoint 本案例實(shí)現(xiàn) 屏幕鼠標(biāo)移動(dòng)轉(zhuǎn)換為視口// 2.屏幕轉(zhuǎn)世界坐標(biāo) public float Speed = 100f;Vector3 Temppos_Worlspos;//接收屏幕轉(zhuǎn)換后的世界坐標(biāo)Vector2 mousePos = new Vector2();//接收獲取到的屏幕坐標(biāo)系2DVector3 Turn_View_Point = new Vector3();//接收轉(zhuǎn)化后的視口坐標(biāo)bool ISrotate = false;bool IsCreatSphere = false;private Camera cam;void Start(){float starTime = Time.time;cam = Camera.main;//如果想要使用主相機(jī) 就可以使用此方法直接獲取主相機(jī), 我們這里主要使用主攝像機(jī)進(jìn)行坐標(biāo)系轉(zhuǎn)換測試}private void Update(){mousePos = Input.mousePosition;//獲取屏幕鼠標(biāo)移動(dòng)坐標(biāo)值Turn_View_Point = cam.ScreenToViewportPoint(new Vector3(mousePos.x, mousePos.y, 100f));//屏幕轉(zhuǎn)視口坐標(biāo)系 100是自定義數(shù)值//_________________________________________________________________________________________________________________Temppos_Worlspos = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 100f));//屏幕轉(zhuǎn)世界坐標(biāo) //_________________________________________________________________________________________________________________//Move(Temppos_Worlspos);Create_Obj_inScreenTurnWorld_Pos(Temppos_Worlspos);//實(shí)現(xiàn)屏幕鼠標(biāo)點(diǎn)擊轉(zhuǎn)換世界坐標(biāo)系然后在這個(gè)位置克隆小球}void OnGUI()//在UNITY的聲明周期中,OnGUI是在Update后面運(yùn)行,每幀運(yùn)行兩次 用于輸出信息{GUILayout.BeginArea(new Rect(20, 50, 500, 300));//創(chuàng)建一塊UI顯示區(qū)域, 標(biāo)簽框GUILayout.Label("獲取到的屏幕鼠標(biāo)坐標(biāo)Mouse position: " + mousePos);GUILayout.Label("給100f的Z值,屏幕坐標(biāo)轉(zhuǎn)換成View視口坐標(biāo)后為:" + Turn_View_Point);GUILayout.Label("————————————————————————————————————");GUILayout.Label("當(dāng)前攝像機(jī)的Z坐標(biāo):" + cam.transform.position.z + "轉(zhuǎn)換世界坐標(biāo)后World position: " + Temppos_Worlspos);GUILayout.EndArea();//結(jié)束UI顯示區(qū)域, 標(biāo)簽框}void Create_Obj_inScreenTurnWorld_Pos(Vector3 Temppos_Worlspos)//實(shí)現(xiàn)屏幕鼠標(biāo)點(diǎn)擊轉(zhuǎn)換世界坐標(biāo)系然后在這個(gè)位置克隆小球{if (Input.GetMouseButtonDown(0)){IsCreatSphere = true;do{if (IsCreatSphere){GameObject FatherPlayer = GameObject.FindGameObjectWithTag("Player");GameObject TempSphereclone = GameObject.CreatePrimitive(PrimitiveType.Sphere);TempSphereclone.transform.SetParent(FatherPlayer.transform);//設(shè)置父物體TempSphereclone.GetComponent<Renderer>().material.color = Color.yellow;//設(shè)置顏色TempSphereclone.transform.position = Temppos_Worlspos;//設(shè)置坐標(biāo)TempSphereclone.transform.localScale = new Vector3(8, 8, 8);//設(shè)置縮放IsCreatSphere = false;Destroy(TempSphereclone, 1f);}} while (false);}}void Move(Vector3 TempV3)//點(diǎn)擊右鍵 移動(dòng){//插值平滑運(yùn)動(dòng)//Vector3 Temp =Vector3.Lerp(transform.position, TempV3, (Time.time- starTime)*Speed);//插值平滑運(yùn)算 (起點(diǎn), 終點(diǎn),起點(diǎn)靠近終點(diǎn)的比例)// this.transform.position = Vector3.Lerp(transform.position, TempV3, (Time.time - starTime) * Speed);// Debug.Log((Time.time - starTime) * Speed +"sudu"+ Temp);// //普通運(yùn)動(dòng)Vector3 Dir = Temppos_Worlspos - transform.position;this.transform.Translate(Dir.normalized * 20 * Time.deltaTime);}
}
? ?3.視口轉(zhuǎn)世界坐標(biāo)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Camera_Class_Two : MonoBehaviour {// 本腳本用于測試 坐標(biāo)系轉(zhuǎn)換Camera OneCam;Vector3 ViewToWorld;Vector3 ViewToScreen;void Start (){OneCam = Camera.main;//這個(gè)可以快速獲取標(biāo)簽為MainCamera 的攝像機(jī)OneCam.transform.position = new Vector3(0,0,-20f);//相機(jī)初始化位置Debug.Log("視口坐標(biāo)(0,0,100)轉(zhuǎn)世界坐標(biāo)系" + OneCam.ViewportToWorldPoint(new Vector3(0, 0, 100f)));//轉(zhuǎn)之后的Z坐標(biāo)是攝像機(jī)原Z坐標(biāo)加上賦值的Z值Debug.Log("視口坐標(biāo)(0.5,0.5,100)轉(zhuǎn)世界坐標(biāo)系" + OneCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 100f)));//轉(zhuǎn)之后的Z坐標(biāo)是攝像機(jī)原Z坐標(biāo)加上賦值的Z值Debug.Log("視口坐標(biāo)(1,1,100)轉(zhuǎn)世界坐標(biāo)系" + OneCam.ViewportToWorldPoint(new Vector3(1, 1, 100f)));//轉(zhuǎn)之后的Z坐標(biāo)是攝像機(jī)原Z坐標(biāo)加上賦值的Z值Debug.Log("————————下面是視口轉(zhuǎn)屏幕————————————————————————" );Debug.Log("視口坐標(biāo)(0,0,100f)轉(zhuǎn)屏幕坐標(biāo)系" + OneCam.ViewportToScreenPoint(new Vector3(0, 0, 100f)));Debug.Log("視口坐標(biāo)(0,0,100f)轉(zhuǎn)屏幕坐標(biāo)系" + OneCam.ViewportToScreenPoint(new Vector3(0.5f, 0.5f, 100f)));Debug.Log("視口坐標(biāo)(0,0,100f)轉(zhuǎn)屏幕坐標(biāo)系" + OneCam.ViewportToScreenPoint(new Vector3(1f, 1f, 100f)));//————————————————————————————世界轉(zhuǎn)視口和平面————————————————}private void OnGUI(){GUILayout.BeginArea(new Rect(50, 20, 800,300));GUILayout.Label("視口坐標(biāo)(0,0,100) 轉(zhuǎn)【世界】坐標(biāo)系" + OneCam.ViewportToWorldPoint(new Vector3(0, 0, 100f))+"Z 等于攝像機(jī)坐標(biāo)+賦值的Z");GUILayout.Label("視口坐標(biāo)(0.5,0.5,100)轉(zhuǎn)【世界】坐標(biāo)系" + OneCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 100f)));GUILayout.Label("視口坐標(biāo)(1,1,100) 轉(zhuǎn)【世界】坐標(biāo)系" + OneCam.ViewportToWorldPoint(new Vector3(1, 1, 100f)));GUILayout.Label("____________________________________________________________");GUILayout.Label("視口坐標(biāo)(0,0,100f)轉(zhuǎn)【屏幕】坐標(biāo)系" + OneCam.ViewportToScreenPoint(new Vector3(0, 0, 100f))+"Z值對屏幕坐標(biāo)系來說并沒有用");GUILayout.Label("視口坐標(biāo)(0,0,100f)轉(zhuǎn)【屏幕】坐標(biāo)系" + OneCam.ViewportToScreenPoint(new Vector3(0.5f, 0.5f, 100f)));GUILayout.Label("視口坐標(biāo)(0,0,100f)轉(zhuǎn)【屏幕】坐標(biāo)系" + OneCam.ViewportToScreenPoint(new Vector3(1f, 1f, 100f)));GUILayout.Label("____________________________________________________________");GUILayout.EndArea();//結(jié)束UI顯示區(qū)域, 標(biāo)簽框}
}
4.向屏幕中間或者視口中間發(fā)射射線
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Camera_Class_ScreenPointToRay : MonoBehaviour {Camera cam;Vector3 RayTargetPoint;Vector3 RayScreen_TargetPoint;void Start(){cam = GetComponent<Camera>();RayTargetPoint = new Vector3(0f,0.5f,0f);}void Update(){RayScreen_TargetPoint = Input.mousePosition;RayTargetPoint.x=RayTargetPoint.x >= 1.0f ? 0.0f : RayTargetPoint.x +0.02f;//數(shù)值增加Ray View_ray = cam.ViewportPointToRay(RayTargetPoint);//朝攝像機(jī)視口上的目標(biāo)點(diǎn)發(fā)射射線Ray Screen_ray = cam.ScreenPointToRay(RayScreen_TargetPoint);//朝向屏幕上某個(gè)點(diǎn)發(fā)射射線//Debug.Log("當(dāng)前射線目標(biāo)點(diǎn)在視口中的位置" + RayTargetPoint);Debug.DrawRay(View_ray.origin, View_ray.direction * 800, Color.yellow);//繪制射線Debug.DrawRay(Screen_ray.origin, Screen_ray.direction * 800, Color.red);//繪制射線RaycastHit hit;//反射探測到的物體if (Physics.Raycast(View_ray, out hit)){print("我看見了物體: " + hit.transform.name);}}
}