简体   繁体   中英

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. The camera is at 0,0,0 and is set to Orthographic. 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.

How should I go about using this to update the objects position without having the Z index affected?

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. Save the value from 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. If the z axis of your GameObjects is 0 , simple overwrite it with 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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