Unity中Camera类实现坐标系转换的示例

1.  用于将世界坐标系转换为屏幕坐标系 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Camer_Class_WorldTo : MonoBehaviour
{

    // 用于将世界坐标系转换为屏幕坐标系
    //本脚本将完成一个案例实现 小球从远处过来
    Transform StarFather;
    Transform StarChild;

    Vector3 ScreenPoint;
    Vector3 ViewPoint;

    Camera OneCamera;
    void Start()
    {
        StarFather = GameObject.FindGameObjectWithTag("Earth").transform;
        OneCamera = Camera.main;//获取主摄像机
    }

    // Update is called once per frame
    void Update()
    {
  
        ScreenPoint = OneCamera.WorldToScreenPoint(StarFather.GetChild(0).transform.position);//世界坐标转换 屏幕坐标
        StarFather.GetChild(0).transform.RotateAround(StarFather.transform.position, Vector3.up, 50 * Time.deltaTime);
        ClickDestory();//实现小球转后的屏幕坐标系和鼠标点击的坐标匹配然后删除该物体

        ViewPoint = OneCamera.WorldToViewportPoint(StarFather.GetChild(0).transform.position);//世界坐标转换 摄像机视口坐标

    }
    private void OnGUI()
    {
        GUILayout.BeginArea(new Rect(50, 20, 500, 300));
        GUILayout.Label("小球的原始坐标:" + (StarFather.GetChild(0).transform.position));
        GUILayout.Label("世界坐标转换 屏幕坐标后" + ScreenPoint);
        GUILayout.Label("鼠标屏幕坐标" + Input.mousePosition);
        GUILayout.Label(("世界坐标转换 视口坐标后" + 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("鼠标碰撞物体成功!!!!!!!!!!!!!!!!!");
            if (Input.GetMouseButtonDown(0))
            {
                Destroy(StarFather.GetChild(0).gameObject);

            }
            Debug.Log("死亡位置!!!!" + ScreenPoint);
        }

    }

}

2. 屏幕坐标转化为视口坐标 Camera.ScreenToViewportPoint和 屏幕转世界坐标    

本案例实现 屏幕鼠标移动转换为视口和屏幕转世界坐标

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Camera_Class : MonoBehaviour
{

    // 用于测试Camera 类 坐标系转换
    //1. 屏幕坐标转化为视口坐标 Camera.ScreenToViewportPoint 本案例实现 屏幕鼠标移动转换为视口
    // 2.屏幕转世界坐标      
    public float Speed = 100f;

    Vector3 Temppos_Worlspos;//接收屏幕转换后的世界坐标
    Vector2 mousePos = new Vector2();//接收获取到的屏幕坐标系2D
    Vector3 Turn_View_Point = new Vector3();//接收转化后的视口坐标

    bool ISrotate = false;
    bool IsCreatSphere = false;
    private Camera cam;

    void Start()
    {
        float starTime = Time.time;
        cam = Camera.main;//如果想要使用主相机 就可以使用此方法直接获取主相机, 我们这里主要使用主摄像机进行坐标系转换测试
    }
    private void Update()
    {
        mousePos = Input.mousePosition;//获取屏幕鼠标移动坐标值

        Turn_View_Point = cam.ScreenToViewportPoint(new Vector3(mousePos.x, mousePos.y, 100f));//屏幕转视口坐标系 100是自定义数值
        //_________________________________________________________________________________________________________________
        Temppos_Worlspos = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 100f));//屏幕转世界坐标      
        //_________________________________________________________________________________________________________________
        //Move(Temppos_Worlspos);
        Create_Obj_inScreenTurnWorld_Pos(Temppos_Worlspos);//实现屏幕鼠标点击转换世界坐标系然后在这个位置克隆小球

    }

    void OnGUI()//在UNITY的声明周期中,OnGUI是在Update后面运行,每帧运行两次 用于输出信息
    {

        GUILayout.BeginArea(new Rect(20, 50, 500, 300));//创建一块UI显示区域, 标签框
        GUILayout.Label("获取到的屏幕鼠标坐标Mouse position: " + mousePos);
        GUILayout.Label("给100f的Z值,屏幕坐标转换成View视口坐标后为:" + Turn_View_Point);

        GUILayout.Label("————————————————————————————————————");
        GUILayout.Label("当前摄像机的Z坐标:" + cam.transform.position.z + "转换世界坐标后World position: " + Temppos_Worlspos);

        GUILayout.EndArea();//结束UI显示区域, 标签框
    }

    void Create_Obj_inScreenTurnWorld_Pos(Vector3 Temppos_Worlspos)//实现屏幕鼠标点击转换世界坐标系然后在这个位置克隆小球
    {
        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);//设置父物体
                    TempSphereclone.GetComponent().material.color = Color.yellow;//设置颜色
                    TempSphereclone.transform.position = Temppos_Worlspos;//设置坐标
                    TempSphereclone.transform.localScale = new Vector3(8, 8, 8);//设置缩放
                    IsCreatSphere = false;
                    Destroy(TempSphereclone, 1f);

                }
            } while (false);
        }

    }


    void Move(Vector3 TempV3)//点击右键 移动
    {
        //插值平滑运动

        //Vector3 Temp =Vector3.Lerp(transform.position, TempV3, (Time.time- starTime)*Speed);//插值平滑运算 (起点, 终点,起点靠近终点的比例)
        //  this.transform.position = Vector3.Lerp(transform.position, TempV3, (Time.time - starTime) * Speed);
        //  Debug.Log((Time.time - starTime) * Speed +"sudu"+ Temp);
        //  //普通运动
        Vector3 Dir = Temppos_Worlspos - transform.position;
        this.transform.Translate(Dir.normalized * 20 * Time.deltaTime);

    }
}

   3.视口转世界坐标

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Camera_Class_Two : MonoBehaviour {

    // 本脚本用于测试 坐标系转换
    Camera OneCam;
    Vector3 ViewToWorld;
    Vector3 ViewToScreen;
    void Start ()
    {
        OneCam = Camera.main;//这个可以快速获取标签为MainCamera 的摄像机
        OneCam.transform.position = new Vector3(0,0,-20f);//相机初始化位置
        Debug.Log("视口坐标(0,0,100)转世界坐标系" + OneCam.ViewportToWorldPoint(new Vector3(0, 0, 100f)));//转之后的Z坐标是摄像机原Z坐标加上赋值的Z值
        Debug.Log("视口坐标(0.5,0.5,100)转世界坐标系" + OneCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 100f)));//转之后的Z坐标是摄像机原Z坐标加上赋值的Z值
        Debug.Log("视口坐标(1,1,100)转世界坐标系" + OneCam.ViewportToWorldPoint(new Vector3(1, 1, 100f)));//转之后的Z坐标是摄像机原Z坐标加上赋值的Z值

        Debug.Log("————————下面是视口转屏幕————————————————————————" );
        Debug.Log("视口坐标(0,0,100f)转屏幕坐标系" + OneCam.ViewportToScreenPoint(new Vector3(0, 0, 100f)));
        Debug.Log("视口坐标(0,0,100f)转屏幕坐标系" + OneCam.ViewportToScreenPoint(new Vector3(0.5f, 0.5f, 100f)));
        Debug.Log("视口坐标(0,0,100f)转屏幕坐标系" + OneCam.ViewportToScreenPoint(new Vector3(1f, 1f, 100f)));
        //————————————————————————————世界转视口和平面————————————————
    }
    
    private void OnGUI()
    {
        GUILayout.BeginArea(new Rect(50, 20, 800,300));
        GUILayout.Label("视口坐标(0,0,100)     转【世界】坐标系" + OneCam.ViewportToWorldPoint(new Vector3(0, 0, 100f))+"Z 等于摄像机坐标+赋值的Z");
        GUILayout.Label("视口坐标(0.5,0.5,100)转【世界】坐标系" + OneCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 100f)));
        GUILayout.Label("视口坐标(1,1,100)      转【世界】坐标系" + OneCam.ViewportToWorldPoint(new Vector3(1, 1, 100f)));
        GUILayout.Label("____________________________________________________________");
        GUILayout.Label("视口坐标(0,0,100f)转【屏幕】坐标系" + OneCam.ViewportToScreenPoint(new Vector3(0, 0, 100f))+"Z值对屏幕坐标系来说并没有用");
        GUILayout.Label("视口坐标(0,0,100f)转【屏幕】坐标系" + OneCam.ViewportToScreenPoint(new Vector3(0.5f, 0.5f, 100f)));
        GUILayout.Label("视口坐标(0,0,100f)转【屏幕】坐标系" + OneCam.ViewportToScreenPoint(new Vector3(1f, 1f, 100f)));
        GUILayout.Label("____________________________________________________________");
        GUILayout.EndArea();//结束UI显示区域, 标签框

    }
}

4.向屏幕中间或者视口中间发射射线

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();
        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;//数值增加
        Ray View_ray = cam.ViewportPointToRay(RayTargetPoint);//朝摄像机视口上的目标点发射射线
        Ray Screen_ray = cam.ScreenPointToRay(RayScreen_TargetPoint);//朝向屏幕上某个点发射射线
        //Debug.Log("当前射线目标点在视口中的位置" + 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);
        }
      
    }
}

你可能感兴趣的:(Unity零基础课程,unity,游戏引擎)