【问题标题】:Unity bullet spawn统一子弹产生
【发布时间】:2016-09-19 11:01:00
【问题描述】:

Where the bullet spawns when I shoot

嗨,由于某些原因,我的子弹不会在我告诉它的地方生成。 生成点在我的枪管末端,我在实例化子弹时采用它的变换位置和旋转,但它生成的位置比枪高(高得多)。 这是我的代码:

    Animation anim;   // Gun animation when shooting
AudioSource gunSound; // Gun sound when firing
public Rigidbody bullet; // I get the rigidbody of the bullet that will be spawned
public Transform spawnPoint; // The position of where it is supposed to spawn


void Start()
{
    anim = GetComponent<Animation>();
    gunSound = GetComponent<AudioSource>();
}

void Update()
{
    if (Input.GetButtonDown("Fire1"))  // If the left mouse button is clicked
    {
        Rigidbody bulletInstance;
        bulletInstance = Instantiate(bullet, spawnPoint.position, spawnPoint.rotation) as Rigidbody; // This is where I don't understand why?!?!
        bulletInstance.AddForce(spawnPoint.forward * 1000f);

        gunSound.Play();
        //anim.Play("GunShot4");
    }
}

帮助:)

【问题讨论】:

  • 所以子弹没有出现在给定变换的位置?是否有任何附加的脚本可能会传送它?
  • 除了在父级的父级上的角色控制器脚本,它不会影响枪的任何东西,不,什么都没有。是的,它没有出现在生成点(空游戏对象)变换处,而是生成得更高,就像在描述(第一行)顶部链接的图片中一样
  • 啊,可能是“公共刚体子弹”需要是“公共游戏对象子弹”,您需要将其实例化为“作为游戏对象”而不是“作为刚体”。您可以使用 gameObject.GetComponent(); 引用刚体

标签: c# unity3d spawning


【解决方案1】:

子弹的原点可能不在模型的中间。

【讨论】:

  • 谢谢,我用球体更改了子弹模型,它可以工作了。我拖放了我的子弹预制件,但看起来原点在正确的位置,但它是固定的 :) 谢谢又来了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-01
  • 2018-01-07
相关资源
最近更新 更多