【发布时间】: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
(); 引用刚体