如何在Unity中实例化对象?

时间:2019-04-05 10:37:11

标签: c# unity3d

我正在Unity中制作自顶向下的游戏,如何从Unity中摄像机视图顶部的玩家当前位置实例化对象?

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

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

但这没用。这只是从相机中心产生的物体。

1 个答案:

答案 0 :(得分:5)

Space只是一个枚举,与您尝试的操作无关! (其目的是为Transform.RotateTransform.Translate定义相对转换空间)

在那个枚举afaik

  • Space.World仅具有整数值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);

看起来有点奇怪。您确定要复制position中实际的player吗?这意味着生成的对象始终与玩家和世界0,0,0处于一条直线上,并且始终是玩家中心的两倍。


对我来说,听起来更像是您想在玩家的前面 生成一些东西……具体取决于玩家的观看方向(在自上而下的游戏中,{{ 1}}或player.transform.up),所以我想您想做的是类似

player.transform.right

相反,它将在播放器对象前面生成对象Instantiate(prefab, player.transform.position + player.transform.forward * 2); Unity单位


发表评论后,听起来好像您想在2上的播放器位置生成对象,而在X-axis上的“上方”生成对象

Y-axis

也许您将不得不调整Instantiate(prefab, player.transform.position + Vector3.Up * 2); ,具体取决于播放器的移动方式以及必须离开屏幕的距离。另外,您也可以使用Camera.ScreenToWorldPoint

2

更新

您说过要在X轴上播放器位置的“对面”生成预制件。

如果您的相机在x = 0的世界上是静态的,那么这实际上很简单:

// 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 = 0上移动,那么我们必须在屏幕位置级别上进行计算:

cameraTopPoint.x = -player.transform.x;

其他更新

好的,所以您希望预制件始终在玩家旁边一定距离处生成。一侧取决于播放器在屏幕的哪一侧,因此您实际上可以再次使用第一种方法,而只需添加所需的距离即可:

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

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