- Home /
Player bullet is fired but spawns ahead of the player character in Unity.
Hi I am making a space invaders style game in C Sharp and when I run the game the bullet takes the position of my player object but it is spawned significantly in front of the player. how do I get the bullet to fire from the same position player ship.
I have included the code associated with the bullet function below. The first section of code is taken from my player class script.
void Update ()
{
horizontalMovement = Input.GetAxis ("Horizontal");
//verticalMovement = Input.GetAxis ("Vertical");
if (Input.GetAxis ("Vertical") > 0 && canFireBullet) {
FireBullet ();
}
}
void FireBullet ()
{
canFireBullet = false;
GameObject.Instantiate (playerBulletPrefab, transform.position, Quaternion.identity);
audio.PlayOneShot (bulletFireSound);
StartCoroutine (ResetCanFireBullet ());
}
IEnumerator ResetCanFireBullet ()
{
yield return new WaitForSeconds(1f);
canFireBullet = true;
}
This section of code is take from the bullet fire class script.
public class bulletfire : MonoBehaviour {
private float movementSpeedMultiplier = 1.5f;
void FixedUpdate ()
{
transform.Translate(Vector3.forward * movementSpeedMultiplier);
}
}
N.B. when I say it takes the position of the player I mean the X and Y positions it is the Z axis that is not aligned.
The code looks fine. Two things to check. Where is the pivot point for your player? Place a small game object at the origin. Then move your player to (0,0,0), is the point from which you fire the bullet the same as the origin? Second check the bullet. Drag the prefab into the scene, move it to (0,0,0). Is the pivot point of the bullet at the origin?
Answer by tiber23 · Oct 10, 2013 at 01:14 PM
that has corrected it cheers I am new to Unity so I didnt think to try that
Your answer
![](https://koobas.hobune.stream/wayback/20220613115618im_/https://answers.unity.com/themes/thub/images/avi.jpg)