Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question bullet not attaching and firing from gun at proper rotation

Discussion in 'Prefabs' started by jamesg12, Feb 23, 2024.

  1. jamesg12


    Aug 23, 2021
    I have a bullet prefab, just a cylinder I rotated 90 degrees:

    it attaches to the gun with this attach point and the attach point is given to the firing mechanism script:

    the firing mechanism component sets the bullet position and rotation to match the attach point and then fires:
    Code (CSharp):
    1.     private void Launch()
    2.     {
    3.         Bullet bullet = bulletPool.GetNextBullet();
    4.         GameObject bulletObject = bullet.GetBulletObject();
    6.         bulletObject.transform.position = startPoint.position;
    7.         bulletObject.transform.rotation = startPoint.rotation;
    8.         ApplyForce(bullet.GetBulletRigidbody());
    9.     }
    11.     private void ApplyForce(Rigidbody rigidBody)
    12.     {
    13.         Vector3 force = startPoint.forward * launchSpeed;
    14.         rigidBody.AddForce(force);
    15.     }
    The problem I am having is the bullet is 'standing up' like if the x rotation on the prefab was set to 0. In fact changing the prefab rotation values doesn't seem to affect how the bullet is fired.

    I know that if I create an object in a 3d modeller and import it so that it is 'on its side' this would solve this problem. However I'm trying to avoid having to learn how to do all that just yet. Is there a way to have the rotation of the bullet match the prefab? Preferably without editing the transform x rotation by 90 degrees every time you fire.
  2. karliss_coldwild


    Oct 1, 2020
    Add an empty object as root of prefab and place the mesh as child of it. Then rotate the transform of child mesh instead of prefab root object.

    You haven't really shown the object hierachy of your bullet but I guess it consists of single object. Setting position/rotation for the prefab root object (in the prefab not the instance) is largely useless in most situations.

    It might be easier to understand if you think about position. If you set the position for prefab root (0, 10, 0). But then after instantiating the prefab you set the bullet object position to (100, 100, 0) it will be (100, 100, 0) not (100, 110, 0). Same happens with rotation, once you change the rotation with `bulletObject.transform.rotation = something` it overwrites whatever rotation you did while editing prefab.

    What you need to do is change the prefab so that forward points forward even when rotation is (0, 0, 0). This can be done by either fixing your 3d model, or adding additional transforms in the hierarchy. Also be careful since the different programs have different conventions for where x,y,z axis point. So there is occasionally settings for how to change the axis during export. If you mess it up and choose wrong combination of export/import settings you will have additional source of problems.
    jamesg12 likes this.
  3. jamesg12


    Aug 23, 2021
    thanks for the info. In the end I just played with the scale of the cylinder so it looked more bullet shaped at 0,0,0 rotation. all my objects are simple stand ins so I will just make sure to keep the orientation in mind when I create or import custom game objects