ana*_*k19 0 c# unity-game-engine
我正在Unity中制作自顶向下的游戏,如何从Unity中摄像机视图顶部的玩家当前位置实例化对象?
到目前为止,我已经尝试过:
Instantiate(prefab, player.transform.position * 2 * Space.World)
Run Code Online (Sandbox Code Playgroud)
但这没用。这只是从相机中心产生物体。
Space
只是一个枚举,与您尝试的内容无关!(它的目的是定义用于例如相对变换空间Transform.Rotate
或Transform.Translate
)
在那个枚举中
Space.World
只是具有int值 0
Space.Self
具有int值 1
所以你实际要做的是
Instantiate(prefab, player.transform.position * 2 * 0);
Run Code Online (Sandbox Code Playgroud)
等于
Instantiate(prefab, Vector3.zero);
Run Code Online (Sandbox Code Playgroud)
这意味着该对象在World位置实例化0,0,0
。
也使用
Instantiate(prefab, player.transform.position * 2);
Run Code Online (Sandbox Code Playgroud)
看起来有点奇怪。您确定要复制的实际position
值player
吗?这意味着生成的对象始终与玩家和世界在一条直线上,0,0,0
并且始终是玩家中心的两倍。
对我来说,听起来更像是您想在玩家面前生成一些东西……取决于玩家的观看方向(通常是在自上而下的游戏中player.transform.up
或player.transform.right
),所以我想您想做的是
Instantiate(prefab, player.transform.position + player.transform.forward * 2);
Run Code Online (Sandbox Code Playgroud)
而是2
在播放器对象前面生成对象Unity单位
您的评论后,它听起来像,而不是你想对球员位置产卵的对象X-axis
,但“在它”在Y-axis
这样
Instantiate(prefab, player.transform.position + Vector3.Up * 2);
Run Code Online (Sandbox Code Playgroud)
也许您必须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);
Run Code Online (Sandbox Code Playgroud)
更新资料
您说过要在X轴上播放器位置的“对面”生成预制体。
如果您的相机在x = 0的世界上是静态的,那么这实际上很简单:
cameraTopPoint.x = -player.transform.x;
Run Code Online (Sandbox Code Playgroud)
如果您的相机是否在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);
Run Code Online (Sandbox Code Playgroud)
其他更新
好的,所以您希望预制件始终在玩家附近一定距离处生成。侧面取决于播放器在屏幕的哪一侧,因此您实际上可以再次使用第一种方法,而只需添加所需的距离即可:
// 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);
Run Code Online (Sandbox Code Playgroud)
在智能手机上输入,因此可能无法“复制粘贴”;)
归档时间: |
|
查看次数: |
662 次 |
最近记录: |