gpt4 book ai didi

c# - 如何在 Unity 中实例化对象?

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

我正在 Unity 中制作一个自上而下的游戏,如何根据 Unity 中摄像机 View 顶部的玩家当前位置实例化对象?

到目前为止我已经尝试过:

Instantiate(prefab, player.transform.position * 2 * Space.World)

但这没有用。这只是从相机中心生成了对象。

最佳答案

Space只是一个枚举,与您正在尝试的内容无关! (其目的是定义相对变换空间,例如 Transform.RotateTransform.Translate )

在那个枚举中

  • Space.World 仅具有 int 值 0
  • Space.Self 的值为 int 1

所以你实际做的是

Instantiate(prefab, player.transform.position * 2 * 0);

等于

Instantiate(prefab, Vector3.zero);

这意味着该对象在世界位置 0,0,0 处实例化。

也使用

Instantiate(prefab, player.transform.position * 2);

看起来有点奇怪。您确定要复制玩家的实际位置吗?这意味着生成的对象始终与玩家和世界0,0,0在一条线上,并且始终与玩家距离中心的距离加倍。


对我来说,这听起来更像是你想要在玩家前面生成一些东西......取决于玩家的 View 方向(在自上而下的游戏中通常player.transform.upplayer.transform.right) 所以我猜你想做的事情是这样的

Instantiate(prefab, player.transform.position + player.transform.forward * 2);

这会在玩家对象前面生成对象 2 个 Unity 单位


在您发表评论后,听起来您想在 X 轴上的玩家位置生成对象,但在 Y 轴上“在其上方”,所以

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);

在智能手机上输入,因此可能无法“复制粘贴”;)

关于c# - 如何在 Unity 中实例化对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55533568/

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