Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Unity 3D中将对象移动到鼠标的z和y坐标_C#_Unity3d - Fatal编程技术网

C# 如何在Unity 3D中将对象移动到鼠标的z和y坐标

C# 如何在Unity 3D中将对象移动到鼠标的z和y坐标,c#,unity3d,C#,Unity3d,有什么帮助吗?这是为了一场比赛,我还有48个小时要完成。谢谢 给你: private void Update() { // Get the mouse position Vector3 mousePos = Input.mousePosition; // Set the z component of the mouse position to the absolute distance between the camera's z and the object's z.

有什么帮助吗?这是为了一场比赛,我还有48个小时要完成。谢谢

给你:

private void Update()
{
    // Get the mouse position
    Vector3 mousePos = Input.mousePosition;

    // Set the z component of the mouse position to the absolute distance between the camera's z and the object's z.
    mousePos.z = Mathf.Abs(Camera.main.transform.position.z - transform.position.z);

    // Determine the world position of the mouse pointer
    Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);

    // Update the position of the object
    transform.position = worldPos;
}

确切地说是什么
transform.position=新矢量3(transform.position.x,worldPos.y,worldPos.z)尝试做什么?尝试
vector3worldpos=Camera.main.ScreenToWorldPoint(新的Vector3(mousePos.x,mousePos.y,transform.position.z))而不是相机的近Clipplane。@HumanWrites它不工作。当我把鼠标移出屏幕中央的一小部分时,worldPos会发疯(在x坐标上是1万亿以上),它一开始不会移动到我的鼠标上,而是移动到另一侧@Putnam它将橙色框(脚本附在其上)移动到worldPos变量的y和z位置(应该是鼠标位置),并保持其x坐标不变。
private void Update()
{
    // Get the mouse position
    Vector3 mousePos = Input.mousePosition;

    // Set the z component of the mouse position to the absolute distance between the camera's z and the object's z.
    mousePos.z = Mathf.Abs(Camera.main.transform.position.z - transform.position.z);

    // Determine the world position of the mouse pointer
    Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);

    // Update the position of the object
    transform.position = worldPos;
}