gpt4 book ai didi

unity-game-engine - 将角色从屏幕的边缘传送到屏幕的边缘

转载 作者:行者123 更新时间:2023-12-01 02:22:29 25 4
gpt4 key购买 nike

我正在尝试将角色从屏幕边缘传送到我正在使用的相反边缘:

var pos: Vector3 = Camera.main.WorldToViewportPoint(transform.position);

if (pos.x < 0.0) {
pos = new Vector3(0.0, 0.0, 0.0);
transform.position = pos;
//Debug.Log("I am left of the camera's view.");
}


if (1.0 < pos.x) {
pos = new Vector3(0.0, 0.0, 0.0);
transform.position = pos;
// Debug.Log("I am right of the camera's view.");
}
if (pos.y < 0.0) Debug.Log("I am below the camera's view.");
if (1.0 < pos.y) Debug.Log("I am above the camera's view.");

这个工作完美,但问题是它将角色传送到中心,当我更改值以使其传送到边缘时,它无法正常工作

最佳答案

问题在于,您将世界坐标 (transform.position) 转换为视口(viewport)空间,进行一些更改,但在将其应用于 transform 之前,切勿从视口(viewport)空间转换回世界空间.位置.

    //you get a world space coord and transfom it to viewport space.
Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);

//everything from here on is in viewport space where 0,0 is the bottom
//left of your screen and 1,1 the top right.
if (pos.x < 0.0f) {
pos = new Vector3(1.0f, pos.y, pos.z);
}
else if (pos.x >= 1.0f) {
pos = new Vector3(0.0f, pos.y, pos.z);
}
if (pos.y < 0.0f) {
pos = new Vector3(pos.x, 1.0f, pos.z);
}
else if (pos.y >= 1.0f) {
pos = new Vector3(pos.x, 0.0f, pos.z);
}

//and here it gets transformed back to world space.
transform.position = Camera.main.ViewportToWorldPoint(pos);

关于unity-game-engine - 将角色从屏幕的边缘传送到屏幕的边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49102831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com