简体   繁体   English

Unity C#ScreenToWorldPoint 2D设置对象Z轴

[英]Unity C# ScreenToWorldPoint 2D setting object Z axis

touchMarkerObjects[fingerId].transform.position = mainCamera.ScreenToWorldPoint(Input.GetTouch(fingerId).position);

When I use this line of code it's setting the touchMarkerObjects Z position to -1308 which makes it not render. 当我使用此行代码时,会将touchMarkerObjects Z位置设置为-1308,这使其无法呈现。 The camera is at 0,0,0 and is set to Orthographic. 相机位于0,0,0并设置为“正交”。 Even if I add a line after or try to use only the X/Y of ScreenToWorldPoint in a Vector3 for the transform.position change it still sets the value to -1308. 即使我在Vector3之后添加一行或尝试仅将ScreenToWorldPoint的X / Y用于transform.position更改,它仍会将值设置为-1308。

How should I go about using this to update the objects position without having the Z index affected? 如何在不影响Z索引的情况下使用它来更新对象位置?

Thanks, I'm certain it's a relatively simple thing I'm overlooking. 谢谢,我确定这是我所忽略的相对简单的事情。 I hope this is the easiest way to relay the issue. 我希望这是解决问题的最简单方法。

On start, get the default z axis of your GameObject. 开始时,获取GameObject的默认z轴。 Save the value from mainCamera.ScreenToWorldPoint(Input.GetTouch(fingerId).position); 保存mainCamera.ScreenToWorldPoint(Input.GetTouch(fingerId).position); to a temporary variable. 到一个临时变量。 Overwrite the z-axis of the result with that original/default variable before using it. 在使用结果之前,请使用该原始/默认变量覆盖结果的z轴。 If the z axis of your GameObjects is 0 , simple overwrite it with 0 . 如果您的GameObjects的z轴为0 ,则简单地用0覆盖它。

float defaultZ = 0f;

Or 要么

float defaultZ = touchMarkerObjects[...].transform.position.z;

then 然后

Vector3 tempValue = mainCamera.ScreenToWorldPoint(Input.GetTouch(fingerId).position);
tempValue.z = defaultZ;
touchMarkerObjects[fingerId].transform.position = tempValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM