BulletPrefab spawning fine but following players movement input.
I'm brand new to all this learning between youtube & unity documentation, so I've only the most basic understanding. I'm making a tiny side scrolling spaceship shooter. My problem is that the bullet prefab spawns in the correct place on the firepoint(empty object parented to player) and begins to move forward fine but it then follows the player up and down(on the x) as it moves, including the players rotation. It should just move forward (along the y axis) and nothing on the x. Anyone see what I'm doing wrong?
//on my player script
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Shoot();
}
}
void Shoot()
{
Instantiate(energyBallPrefab, firePoint);
}
//on bullet prefab script
{
public float bulletSpeed = 20f;
public Rigidbody2D rbBullet;
private void Start()
{
rbBullet.velocity = new Vector2(0, 1 * bulletSpeed);
}`
Answer by SpaceManDan · Apr 20, 2021 at 04:15 AM
The problem is that the firepoint is parented to the player. This makes the spawned object a child of the player which means it will be bound to players movement.
Several solutions here. Easiest is to instantiate in world space. Here is how Unity has structured that for you. Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
So, in practice here's what you need to do. Change Instantiate(energyBallPrefab, firePoint) by adding the worldspace bool like so
Instantiate(energyBallPrefab, firePoint, true);
Or, another way you can do this is adding this as the first line in your Start Method.
rdBullet.transform.parent = null;
That will make sure it's not connected to anything as it will not be a child of any other object. So pick one or the other and use one. I think the first method is the best but in programming saying this is the "best way" is often a great way of learning to box yourself in and lose sight of "making it work". So pick what works best for you.
Both worked perfectly, I've gone with the latter for now. Thank you very much friend. Simple and concise. Was struggling to even find the cause myself.
Your answer
![](https://koobas.hobune.stream/wayback/20220613032533im_/https://answers.unity.com/themes/thub/images/avi.jpg)