物品跟随鼠标移动在透视角与正交视角的情况

物品跟随鼠标移动

 

正交视角

    void Update()
    {
        Vector3 v3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = new Vector3(v3.x, v3.y, 0); 
    }

 

 

透视角

    void Update()
    {
        Vector3 v3 = GetWorldPositionOnPlane(Input.mousePosition);
        transform.position = new Vector3(v3.x, v3.y, 0); 
    }
    
    public Vector3 GetWorldPositionOnPlane(Vector3 screenPosition, float z = 0) {
        Ray ray = Camera.main.ScreenPointToRay(screenPosition);
        Plane xy = new Plane(Vector3.forward, new Vector3(0, 0, z));
        float distance;
        xy.Raycast(ray, out distance);
        return ray.GetPoint(distance);
    }

 

posted @ 2020-05-15 21:46  三页菌  阅读(46)  评论(0编辑  收藏