5327人加入学习
(15人评价)
3D数学基础:Unity游戏开发

制作于2018.2.4,基于Unity2017.2。

价格 ¥ 144.00
该课程属于 Unity - A计划(永久有效期) 请加入后再学习

### 屏幕坐标与视口坐标转换

 

#### Camera.ScreenToViewportPoint

 

public Vector3 ScreenToViewportPoint(Vector3 position);

 

Transforms position from screen space into viewport space.

 

Screenspace is defined in pixels. The bottom-left of the screen is (0,0); the right-top is (pixelWidth,pixelHeight). The z position is in world units from the camera.


 

#### Camera.ViewportToScreenPoint

 

public Vector3 ViewportToScreenPoint(Vector3 position);

 

Transforms position from viewport space into screen space.

 

Viewport space is normalized and relative to the camera. The bottom-left of the camera is (0,0); the top-right is (1,1). The z position is in world units from the camera.

[展开全文]
    private void Update()
    {
        //if (timer <= 1)
        {
            m_go1.transform.Translate(Vector3.right * 2 * Time.deltaTime);
            Vector3 viewPos = Camera.main.WorldToViewportPoint(m_go1.transform.position);
            if (viewPos.x >= 1f || viewPos.x < 0.0 || viewPos.y >= 1f || viewPos.y < 0.0)
            {
                m_go1.transform.position = oldPos;
                return;
            }
            oldPos = m_go1.transform.position;
            timer += Time.deltaTime;
        }
    }

 

[展开全文]