Bullet firing "stood up" and not at the correct rotation.

Hey the shooting script I am using has a little problem. The problem is when I fire the bullet from the spawn point, it fires vertical despite the fact the bullet prefab is facing the correct way. Basically the bullet is coming out of the barrel as though it is stood up and not facing forward, I can’t see what I’m doing wrong. Is it anything to do with the rigidbody or am I missing something in this:

var bulletPrefab:Transform;
var shotDelay = 2;
     
         function Start () {

    while (true) {
        while (!Input.GetButtonDown("Jump")) yield;
       bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, transform.rotation);
         bullet.rigidbody.AddForce(bullet.transform.forward * 5000);
        yield WaitForSeconds(shotDelay);
    }
}

Any help is appreciated :slight_smile: If you need anymore information feel free to ask.

1 Answer

1

As phodges says, it’s an inconsistant orientation problem.

In Unity, forward is officially the blue Z arrow (right is the red X arrow, up is the green Y arrow.) This means that your line AddForce(bullet.forward); pushes the bullet along it’s current blue arrow. I’m going to guess, if you drag the a bullet into the scene, that the bullet’s blue forward arrow is really pointing up.

One easy solution is to fire the bullet whichever way it thinks is forward. If the red arrow is pointing to the tip of the bullet, the tip is officially “bullet right” so use AddForce(bullet.rght);

That can be confusing (since other bullets may have to fire down or left.) Most people force everything so that forward-looking (like the bullet tip) really is forward, using childing. The “bullet” is a parent with everything except the model, with the bullet model childed. That allows you to spin the bullet so it’s tip and the parent blue arrow line up. I’m going to guess your gun has the same issue – the barrel probably faces the same “wrong” way as the bullet.

Thanks for the comment, I feel I understand what you're saying. When I say the bullet fires "vertical" I mean it goes forward, but the bullet is actually erected and not horizontal. As for the childing, how would I make a prefab a child? Would it be done in the hierarchy?

Your information about the axis really helped me fix this. I fixed the problem within my modelling software, I simply rotated the pivot in the software and well... That's it, thanks buddy.

To play with parenting on a prefab, which it sounds like you don't need to do anymore: drag it into the scene, rearrange things, then drag it back. The entire parent-child still counts as one spawnable prefab.