【问题标题】:Find out camera world width and height?找出相机世界的宽度和高度?
【发布时间】:2019-09-22 02:36:39
【问题描述】:

简而言之,这是我的问题: 我想知道如何在屏幕中间生成一个对象,这意味着我想以世界单位查找屏幕的宽度和高度值。

相机位置:0,23,-10

相机旋转:60,0,0

所以我试过这个:

Vector3 screenWorldSize = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, -10));

// In the Update function
If(Input.GetKeyDown(Keycode.P))
{
GameObject tempObject = Instantiate(gamePrefab);
tempObject.transform.position = new Vector3(screenWorldSize.x, 1.5f, 10f);
}

但是 tempObject 的位置不正确(不在中间),所以我尝试Debug.Log(screenWorldSize) 得到了这个结果(-10.3, 28.8, -20.0)

我尝试将Camera.main.ViewPortToWorldPoint()(0,0,-10)(1,0,-10) 一起使用,结果几乎相同。

这是现场的图像..

有什么我不明白的地方吗? 如何获取世界点的屏幕边缘?

【问题讨论】:

  • 嗨。它不是重复的。请再读一遍问题我的问题根本不是关于ui的,你的问题根本无法解决我的问题。
  • 嗨。这对你不起作用? tempObject.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 10)
  • 您好,很遗憾,没有!这就是为什么我需要更多帮助。相信我,我已经阅读了几乎所有关于这个问题的问题/帖子/博客,但没有一个有效。我告诉你这不是重复的。

标签: c# android unity3d camera screen


【解决方案1】:

非常简单。你根本不需要宽度和高度,你只需要方向。

// this is a vector matching the camera's direction
Vector3 cameraDirection = Camera.main.transform.rotation * Vector3.forward;
// this is the camera position
Vector3 cameraPosition = Camera.main.transform.position;

如果你只想创建一个距离相机一定距离的对象,你可以这样做来获取它的位置

Vector3 position = cameraPosition + cameraDirection * distance;

如果你想在相机指向的另一个对象上生成对象,你需要这个:

RaycastHit hit;
if (Physics.Raycast(cameraPosition, cameraDirection, out hit))
{
    Vector3 position = hit.point;
    [..]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多