Unity3d 我如何将我的游戏对象立即移动到屏幕上被触摸的部分?
我有麻烦了。我想简单地移动我的游戏对象立即(没有动画)到屏幕的一部分是触摸。我不希望这个物体可以拖动。我只希望用户能够通过触摸屏幕上的区域而不是拖动来移动对象。在我的代码中,它正在检测触摸,但只要我触摸屏幕上的某个地方,物体就会消失(在我的iOS手机上测试)。这是一个2D游戏。我不确定这是否会影响它,但在我的游戏中,相机总是向下移动 这在my Update()函数中:Unity3d 我如何将我的游戏对象立即移动到屏幕上被触摸的部分?,unity3d,unity5,Unity3d,Unity5,我有麻烦了。我想简单地移动我的游戏对象立即(没有动画)到屏幕的一部分是触摸。我不希望这个物体可以拖动。我只希望用户能够通过触摸屏幕上的区域而不是拖动来移动对象。在我的代码中,它正在检测触摸,但只要我触摸屏幕上的某个地方,物体就会消失(在我的iOS手机上测试)。这是一个2D游戏。我不确定这是否会影响它,但在我的游戏中,相机总是向下移动 这在my Update()函数中: if(Input.touchCount > 0 && Input.GetTouch(0).
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 0));
transform.position = touchPosition;
}
我想问题是您指定了0作为深度 让C做你的相机。只要对象不是太远(d>far),也不是太近(d
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(
new Vector3(touch.position.x, touch.position.y, 0));
您基本上说:我需要找到一个位于光线上的点,从C(Camera.main
)开始,向M(touch.position
)移动。要获取该光线上的特定点,必须指定沿该光线的距离:d。当您将d设置为0时,您得到的正是C,它不仅离相机太近,而且实际上在相机上d触摸位置的任何对象都不会被渲染
float d = Camera.main.nearClipPlane + 2f; // added some buffer to accommodate for object dimensions
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(
new Vector3(touch.position.x, touch.position.y, d));
长话短说:确保近,您将获得一个触摸位置
,您可以在其上放置对象并进行渲染
float d = Camera.main.nearClipPlane + 2f; // added some buffer to accommodate for object dimensions
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(
new Vector3(touch.position.x, touch.position.y, d));
我想问题是您指定了0作为深度
让C做你的相机。只要对象不是太远(d>far),也不是太近(d),相机就会观察空间并渲染它能看到的所有对象。
你打电话的时候
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(
new Vector3(touch.position.x, touch.position.y, 0));
您基本上说:我需要找到一个位于光线上的点,从C(Camera.main
)开始,向M(touch.position
)移动。要获取该光线上的特定点,必须指定沿该光线的距离:d。当您将d设置为0时,您得到的正是C,它不仅离相机太近,而且实际上在相机上d适用,因此该触摸位置的任何对象都不会被渲染
float d = Camera.main.nearClipPlane + 2f; // added some buffer to accommodate for object dimensions
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(
new Vector3(touch.position.x, touch.position.y, d));
长话短说:确保近,您将获得一个触摸位置
,您可以在其上放置对象并进行渲染
float d = Camera.main.nearClipPlane + 2f; // added some buffer to accommodate for object dimensions
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(
new Vector3(touch.position.x, touch.position.y, d));