在将预制件实例化到所需位置时需要帮助

时间:2019-08-08 20:25:26

标签: c# unity3d

我正在尝试在2D游戏中实例化子弹。但是子弹正好在我的播放器(2D飞碟)的中心产生。我尝试向其中添加一个vector3,但项目符号似乎没有使用它。它产生错误,但旋转正确。

     Instantiate(bulletPrefab, Player.transform.position, 
      Player.transform.rotation);

3 个答案:

答案 0 :(得分:1)

这样做的原因是因为Player.transform.position行实际上是"Spawn my prefab, at the location of my player, with that rotation of my player"

实例化实际上是三个部分 What do I want to spawn? Where do I want to put it? What rotation should it have?

因此,您希望对代码进行重新处理以使其位于所需的位置和旋转位置。

Instantiate(bulletPrefab, desiredSpawnPos, desiredRotation)

这将在所需的位置以所需的旋转生成您的预制件。

答案 1 :(得分:0)

您可以通过此操作选择产卵位置

.featured-block__item {
  flex-basis: calc(25% - 2rem);
  width: calc(25% - 2rem);
}

.featured-block__image img {
  width: 100%;
}

.featured-block a {
  animation-name: cf4FadeInOut;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-duration: 16s;
  position: absolute !important;
  opacity: 0;
}

.featured-block a:nth-of-type(1) {
  animation-delay: 0;
}

.featured-block a:nth-of-type(2) {
  animation-delay: 4s;
}

.featured-block a:nth-of-type(3) {
  animation-delay: 8s;
}

.featured-block a:nth-of-type(4) {
  animation-delay: 12s;
}

@keyframes cf4FadeInOut {
  0% {opacity: 0;}
    20% {opacity: 1;z-index: 999;}
    33% {opacity: 1;}
    53% {opacity: 0;z-index: 1;}
    100% {opacity: 0;}
}

答案 2 :(得分:0)

谢谢大家,我找到了解决方案。 所以我做了一个孩子,让它成为了产生子弹的位置。

    Transform Spawnposition;

    Spawnposition =go.transform.FindChild("Fire");

    bulletInstance = Instantiate(bulletPrefab,Spawnposition.transform.position, 
    go.transform.rotation);

PS。我的播放器也是预制的。