问题描述:

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;
相关阅读:
Top