获取鼠标在世界空间中的位置 [英] Get mouse position in world space

查看:28
本文介绍了获取鼠标在世界空间中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Unity 制作游戏,但我有一个小问题,我需要知道世界空间中的鼠标位置,为此我尝试使用以下代码在鼠标位置设置一个游戏对象:

I'm making a game using Unity and I have a little issue, I need to know the mouse position in world space, for that I try to set a GameObject at the mouse position using this code :

    Vector3 p = Input.mousePosition;
    Vector3 pos = Camera.main.ScreenToWorldPoint( p);
    testGameObject.transform.position = pos;

它在编辑器中就像一个魅力,但在 exe/apk 中,游戏对象不跟随鼠标:

It works like a charm in Editor but in exe / apk, the GameObject dosn't follow the mouse:

示例 1

示例 2

应该跟随鼠标的游戏对象是圆圈内的1"

The GameObject that is supposed to follow the mouse is the "1" inside a circle

推荐答案

如果它在编辑器中像魅力一样工作,那么它应该在构建中.

If it's works in editor like a charm, then it should in build to.

我看到它已经在构建中工作了,也许您想要的是准确放置您的对象以匹配屏幕点击点,但是您的对象离相机太近以至于您看不到它.

I see it already working in build, maybe what you want is to exactly place yor object to match the screen click point, but your object is far too close to the camera so that you can't see it.

可能是距离相机的位置深度有问题.尝试添加类似

maybe there's a problem with the depth of the position from the camera. try adding something like

Vector3 p = Input.mousePosition;
p.z = 20;
Vector3 pos = Camera.main.ScreenToWorldPoint(p);
testGameObject.transform.position = pos;

为鼠标位置添加深度.如果仍然不起作用,请尝试将 20 更改为 -20

To add depth to the mouse position. try to change 20 to -20 if it's still doesn't work

这篇关于获取鼠标在世界空间中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆