简体   繁体   English

如何在Unity中实例化对象?

[英]How do I Instantiate Objects In Unity?

I am making a top down game in Unity, how do I instantiate objects based on the players current position from the top of the camera view in Unity? 我正在Unity中制作自上而下的游戏,如何从Unity中摄像机视图顶部的玩家当前位置实例化对象?

So far I have tried: 到目前为止,我已经尝试过:

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

but this didn't work. 但这没用。 This just spawned objects from the center of the camera. 这只是从相机中心产生物体。

Space is simply an enum and has nothing to do with what you are trying! Space只是一个枚举,与您尝试的内容无关! (Its purpose is to define the relative transform space for eg Transform.Rotate or Transform.Translate ) (其目的是为例如Transform.RotateTransform.Translate定义相对转换空间)

in that enum afaik 在那个枚举中

  • Space.World simply has the int value 0 Space.World的int值为0
  • Space.Self has the int value 1 Space.Self的int值为1

so what you actually do is 所以你实际要做的是

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

which equals 等于

Instantiate(prefab, Vector3.zero);

which means the object is instantiated at World position 0,0,0 . 这意味着该对象在世界位置0,0,0处实例化。

Also using 也使用

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

looks a bit strange. 看起来有点奇怪。 Are you sure you want to duplicate the actual position of the player ? 您确定要复制player的实际position吗? This would mean the spawned object is always on a line with the player and the World 0,0,0 and always double as far from the center as the player. 这意味着生成的对象始终与玩家和世界0,0,0处于一条直线上,并且始终是玩家中心的两倍。


To me it sounds more like you rather want to spawn something in front of the player ... depending on the player's view direction (in topdown games usually player.transform.up or player.transform.right ) so I guess what you are trying to do instead is something like 对我来说,听起来更像是您想在玩家面前生成一些东西……取决于玩家的观看方向 (在自上而下的游戏中,通常是player.transform.upplayer.transform.right ),所以我猜您正在尝试什么相反,它就像

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

which would instead spawn the object 2 Unity units in front of the player object 而是在播放器对象前面生成对象2 Unity单位


After your comment it sounds like instead you want to spawn the object at the player position on the X-axis but "over it" on the Y-axis so 您的评论后,它听起来像,而不是你想对球员位置产卵对象X-axis ,但“在它”在Y-axis这样

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

maybe you'll have to tweak the 2 depending how your player can move and how far it has to be to be "off screen". 也许您需要根据播放器的移动方式以及“屏幕外”的移动距离来调整2 Alternatively you could also use a bit more complex using Camera.ScreenToWorldPoint 或者,您也可以使用Camera.ScreenToWorldPoint使用Camera.ScreenToWorldPoint复杂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);

Update 更新资料

you said you want the prefab to be spawned on the "opposide" of the player position on the X axis. 您说过要在X轴上播放器位置的“对面”生成预制体。

If your Camera is static on world x=0 than this is actually quite simple: 如果您的相机在x = 0的世界上是静态的,那么这实际上很简单:

cameraTopPoint.x = -player.transform.x;

If your camera is moving or not on x=0 than we have to calculate it already on the screen position level: 如果您的相机是否在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);

Other Update 其他更新

Ok so you want the prefab always spawn next to the player in a certain distance. 好的,所以您希望预制件始终在玩家附近一定距离处生成。 The side depends on which side of the screen the player is so you could actually use the first approach again but just add the desired distance: 侧面取决于播放器在屏幕的哪一侧,因此您实际上可以再次使用第一种方法,而只需添加所需的距离即可:

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

Typed on smartphone so might not be "copy-paste-able" ;) 在智能手机上输入,因此可能无法“复制粘贴”;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Unity-如何实例化资产? - Unity - How do I instantiate an asset? 您如何使用实例化连续生成统一的对象? - How do you consecutively spawn objects in unity using instantiate? 如何在 Unity 中另一个游戏对象的转换 position 实例化 object? - How do I Instantiate an object at the transform position of another GameObject in Unity? 如何在 Unity 中以最佳方式实例化 UI 图像的静态网格? - How do I instantiate a static grid of UI images in Unity optimally? 如何实例化可变数量的对象? (C#) - How do I Instantiate a variable number of objects? (C#) 如何在 Unity (C#) 中实例化游戏对象以及实例化的 integer? - How can I instantiate game objects in Unity (C#) along with an instantiated integer? 如何在没有交集的区域中实例化多个对象? (统一) - How to instantiate multiple objects in area without intersection? (Unity) 如何在Unity Game Engine平台的单元测试中实例化MonoBehaviour对象 - How to instantiate MonoBehaviour objects in a unit test on Unity Game Engine platform 如何/在哪里使用PRISM和Unity实例化MVVM中的模型对象 - How/Where to instantiate model objects in MVVM using PRISM and Unity 如何通过 Unity C# 中的脚本正确实例化预制件? - How do I instantiate a prefab through a script in Unity C# correctly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM