1

I want to place gameobject to same place where I pressed with mouse in X,Y coordinates. Z should be the same always

How can I achieve this?

I tried currently

Vector3 a = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
myGameObject.transform.position = Vector3.Lerp(myGameObject.transform.position, a, 0.01f);

But this doesn't work. It just moves my object in Z coordinate.

4

1 回答 1

0

您将 z 轴更改为 0。您应该像这样保持原始 z 坐标:

Vector3 a = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));

a.Set(a.x, a.y, myGameObject.transform.position.z);

myGameObject.transform.position = Vector3.Lerp(myGameObject.transform.position, a, 0.01f);
于 2014-11-09T21:25:00.753 回答