我正在 Unity 中制作自上而下的游戏,如何根据玩家当前位置从 Unity 中的相机视图顶部实例化对象?
到目前为止,我已经尝试过:
Instantiate(prefab, player.transform.position * 2 * Space.World)
但这没有用。这只是从相机的中心生成对象。
Space
只是一个枚举,与您正在尝试的无关!(其目的是为 egTransform.Rotate
或定义相对变换空间Transform.Translate
)
在那个枚举 afaik
Space.World
只是具有 int 值 0
Space.Self
具有 int 值 1
所以你实际上做的是
Instantiate(prefab, player.transform.position * 2 * 0);
等于
Instantiate(prefab, Vector3.zero);
这意味着对象在 World 位置实例化0,0,0
。
还使用
Instantiate(prefab, player.transform.position * 2);
看起来有点奇怪。您确定要复制 的实际position
值player
吗?这意味着生成的对象总是与玩家和世界在一条线上,并且0,0,0
离中心的距离总是与玩家一样远。
对我来说,这听起来更像是你宁愿在玩家面前产生一些东西......取决于玩家的视线方向(通常在自上而下的游戏中player.transform.up
或player.transform.right
)所以我猜你想要做的是
Instantiate(prefab, player.transform.position + player.transform.forward * 2);
这将2
在玩家对象前面生成对象Unity 单位
您的评论后,它听起来像,而不是你想对球员位置产卵的对象X-axis
,但“在它”在Y-axis
这样
Instantiate(prefab, player.transform.position + Vector3.Up * 2);
也许您必须2
根据播放器的移动方式以及“屏幕外”的距离来调整。或者,您也可以使用更复杂的使用Camera.ScreenToWorldPoint
// get players position
var playerPos = player.transform.position;
// get camera's position
var cameraPos = Camera.main.transform.position;
// get difference on Z-axis
var cameraDistance = cameraPos.z - playerPos.z;
// Get the world 3d point for the upper camera border
// don't care about the X value
// and as distance we use the z-distance to the player
var cameraTopPoint = Camera.main.ScreenToWorldPoint(new Vector3(0, Camera.main.pixelHeight, cameraDistance);
// use the players X-axis position
cameraTopPoint.x = player.transform.x;
// to spawn it exactly on the Screen border above the player
Instantiate(prefab, cameraTopPoint);
// or to spawn it a bit higher
Instantiate(prefab, cameraTopPoint + Vector3.Up * 1);
更新
您说您希望预制件在 X 轴上玩家位置的“对面”生成。
如果您的相机在世界 x=0 上是静态的,那么这实际上非常简单:
cameraTopPoint.x = -player.transform.x;
如果您的相机是否在 x=0 上移动,那么我们必须已经在屏幕位置级别计算它:
// get players position
var playerPos = player.transform.position;
// additionally convert the player position to screen space
var playerScreenPos = Camera.main.WorldToScreenPoint(playerPos);
var centerX = Camera.main.pixelWidth / 2.0f;
// get the difference between camera an player X (in screen space)
var differenceX = Mathf.Abs(playerScreenPos.x - centerX);
// here calculate the opposide side
var targetX = centerX + differenceX * (playerScreenPos.x < centerX ? 1 : -1);
// or alternatively if you want e.g. that the prefab always
// spawn with a bit of distance to the player even though he is very close
// to the center of the screen you could do something like
//var targetX = centerX + centerX / 2 * (playerScreenPos.x < centerX ? 1 : -1);
// Get the world 3d point for the upper camera border
// with the calculated X value
// and as distance we use the z-distance to the player
var cameraTopPoint = Camera.main.ScreenToWorldPoint(new Vector3(targetX, Camera.main.pixelHeight, playerScreenPos.z);
// to spawn it exactly on the Screen border above the player
Instantiate(prefab, cameraTopPoint);
// or to spawn it a bit higher
Instantiate(prefab, cameraTopPoint + Vector3.Up * 1);
其他更新
好的,所以您希望预制件始终在玩家旁边一定距离处生成。一侧取决于玩家在屏幕的哪一侧,因此您实际上可以再次使用第一种方法,但只需添加所需的距离:
// Adjust this is the Inspector (in Unity units)
public float spawnDistanceToPlayer;
...
// get players position
var playerPos = player.transform.position;
// additionally convert the player position to screen space
var playerScreenPos = Camera.main.WorldToScreenPoint(playerPos);
var centerX = Camera.main.pixelWidth / 2.0f;
// Get the world 3d point for the upper camera border
// with the calculated X value
// and as distance we use the z-distance to the player
var cameraTopPoint = Camera.main.ScreenToWorldPoint(new Vector3(playerScreenPos.x, Camera.main.pixelHeight, playerScreenPos.z);
// now add or reduce spawnDistanceToPlayer
// depending on which side of the screen he is
cameraTopPoint.x += spawnDistanceToPlayer * (playerScreenPos.x < centerX ? 1 : -1);
// OR if your camera sits static on X=0 anyway you also could compare
// the player position directly without calculating in screenspace:
cameraTopPoint.x += spawnDistanceToPlayer * (playerPos.x < 0 ? 1 : -1);
// to spawn it exactly on the Screen border above the player
Instantiate(prefab, cameraTopPoint);
// or to spawn it a bit higher
Instantiate(prefab, cameraTopPoint + Vector3.Up * 1);
在智能手机上输入,因此可能无法“复制粘贴”;)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句