ScreenToWorldPoint在Unity中做了一些奇怪的事情

时间:2018-06-16 18:12:18

标签: unity3d coordinate-systems coordinate-transformation

我正在尝试从Unity中的屏幕坐标转换为世界坐标,但失败了。

我将第三个坐标视为距离相机according to documentation的距离并尝试了

public class BaseDbContext : DbContext
{
    public BasetDbContext(DbContextOptions options)
            : base(options) { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (WebHelper.ConnectionString != null)
        {
            optionsBuilder.UseSqlServer(WebHelper.ConnectionString);
        }
    }
}

但结果总是

 GetComponent<Camera>().ScreenToWorldPoint(new Vector3(0, 0, 0))
 GetComponent<Camera>().ScreenToWorldPoint(new Vector3(100, 0, 0))
 GetComponent<Camera>().ScreenToWorldPoint(new Vector3(0, 100, 0))
 GetComponent<Camera>().ScreenToWorldPoint(new Vector3(10000000, 0, 0))
 GetComponent<Camera>().ScreenToWorldPoint(new Vector3(0, 10000000, 0))

其中 (0.0, 0.0, 2.0) 是相机对象的坐标。

我发现,如果第三个坐标为零,则前两个坐标无效。如果第三个坐标为(0.0, 0.0, 2.0),则效果存在,但与第三个坐标为1时不同。

这是设计吗?

1 个答案:

答案 0 :(得分:0)

问题在于定义变量。

改变这个:

float mouseX = -Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");

为此:

float mouseX = Input.mousePosition.x;
float mouseY = Input.mousePosition.y;

您正在尝试获取鼠标轴,但实际上您需要获取鼠标位置。

相关问题