ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Unity论坛问答-如何让Camera.ScreenToWroldPoint在XZ平面上移动

2020-07-09 21:35:37  阅读:284  来源: 互联网

标签:hitdist ScreenToWroldPoint Vector3 zero plane XZ Unity Camera return


原贴地址: How to make "Camera.ScreenToWroldPoint" work on XZ plane?

题主luwenwan问道:

public class DragOnPlanXY : MonoBehaviour
{
    void Update()
    {
        var screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
        transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    }
}

这个代码可以让物件随着鼠标在XZ平面上移动, 但是怎么能让物件在XY平面上移动呢?

neginfinity提供了一个使用Vector3.ProjectOnPlane的方法

using UnityEngine;

public class DragByProjectOnPlane : MonoBehaviour
{
    float timer = 0;

    void Update()
    {
        if (timer > 0)
        {
            timer--;
            return;
        }

        timer = 120;

        var screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
        var targetTransform = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));

        Vector3 planePoint = Vector3.zero;//point on the plane
        Vector3 planeNormal = Vector3.up;//normal vector of the plane
        transform.position = Vector3.ProjectOnPlane(targetTransform - planePoint, planeNormal) + planePoint;
    }
}

当然他只提供了最后3行代码, 这个方法问题就是与镜头的深度无法立即获得, 所以能明显感到物件有一个延时.
这里加了一个计数器能更清楚地看到这个问题.

exiguous提供了一个使用Plane.RayCast的方法

public static class ExtensionMethods_Camera
{
    private static Plane xzPlane = new Plane(Vector3.up, Vector3.zero);

    public static Vector3 MouseOnPlane(this Camera camera)
    {
        // calculates the intersection of a ray through the mouse pointer with a static x/z plane for example for movement etc, 
        // source: http://unifycommunity.com/wiki/index.php?title=Click_To_Move
        Ray mouseray = camera.ScreenPointToRay(Input.mousePosition);
        float hitdist = 0.0f;
        if (xzPlane.Raycast(mouseray, out hitdist))
        {
            // check for the intersection point between ray and plane
            return mouseray.GetPoint(hitdist);
        }
        if (hitdist < -1.0f)
        {
            // when point is "behind" plane (hitdist != zero, fe for far away orthographic camera) simply switch sign 
            // https://docs.unity3d.com/ScriptReference/Plane.Raycast.html
            return mouseray.GetPoint(-hitdist);
        }
        // both are parallel or plane is behind camera so write a log and return zero vector
        Debug.Log("ExtensionMethods_Camera.MouseOnPlane: plane is behind camera or ray is parallel to plane! " + hitdist);       
        
        return Vector3.zero;
    }
}

 

标签:hitdist,ScreenToWroldPoint,Vector3,zero,plane,XZ,Unity,Camera,return
来源: https://www.cnblogs.com/yusjoel/p/13276345.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有