I have started a space shooter game for practice on Unity 2D games.
In order to fire a bullet upwards, I am instantiating my bullet prefab at the ships position plus an additional value in the Y direction to make it appear to come from the ship’s nose.
The bullet prefab is instantiated when I push the space bar.
The code to achieve this looks like this;
if(Input.GetButtonDown("Jump") && elapsedTime > reloadTime) {
Vector3 spawnPosition = transform.position;
spawnPosition += new Vector3(0, 0.8f, 0));
Instantiate(bulletPrefab, spawnPosition , Quaternion.identity);
}
This works fine, but to clean up the code a little, I then decided to combine two of the lines as follows;
if(Input.GetButtonDown("Jump") && elapsedTime > reloadTime) {
Vector3 spawnPosition = transform.position += new Vector3(0, 0.8f, 0));
Instantiate(bulletPrefab, spawnPosition , Quaternion.identity);
}
When I do this, the player component that I have this script attached to moves up the screen to the same point as the bullet prefab instantiation point.
I can’t understand why. There is nothing telling the parent component to have its position updated to this point. Can someone explain to me why this would happen?
Thanks in advance!