Search Unity

Prefab Rotation After Instantiating

Discussion in 'Scripting' started by Marikc, Jan 23, 2017.

  1. Marikc

    Marikc

    Joined:
    Jan 10, 2017
    Posts:
    21
    Fixed. Found that you can create an empty GameObject and place the projectile as a child then create a prefab from that with the correct rotation. I wish you could simply change the rotation of the x, y or z axis on an object without rotating the object.

    I've spent a lot of hours on this issue, researching, asking questions and watching videos.

    There is a an Empty GameObject attached to the ship on all four turrets. These are called turretLB, turretLT, etc (left bottom, left top). These GameObjects are attached to the script in the inspector. This is the instantiation script as it currently exists:

    Code (csharp):
    1. Instantiate(laserLBPrefab, turretLB.position, turretLB.transform.rotation);
    2. Instantiate(laserLTPrefab, turretLT.position, turretLT.transform.rotation);
    3. Instantiate(laserRBPrefab, turretRB.position, turretRT.transform.rotation);
    4. Instantiate(laserRTPrefab, turretRT.position, turretRT.transform.rotation);
    On the prefab there is a script to move the projectile forward:

    Code (csharp):
    1. transform.Translate(transform.forward * maxSpeed * Time.deltaTime, Space.World);
    I had to add Space.World to get it to move forward in the correct direction, though I am confused as to why you use World coordinates and not Local.

    The issue is that the prefab projectile comes out at a 90 degree angle pointing straight up. Changing the rotation of the Empty GameObject (turretLB, turretLT, etc) has no effect.

    Any suggestions? I've tried dozens of things to fix this.

     
    Last edited: Jan 23, 2017
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    which prefab is that attached to, the projectile? what does the instantiation code for the projectile look like?
     
  3. ksnkfs

    ksnkfs

    Joined:
    Nov 30, 2014
    Posts:
    35
    Im a very news in programming but should not you do turretLB.rotation?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    well usually the code attached to the projectile just says "go forward", which makes it totally dependent on the initial orientation it was instantiated with. I'm guessing a little that the prefab is being instantiated with a reference to it's own rotation not that of the spawn point given that it's not changing when the spawn point is altered.
     
  5. Marikc

    Marikc

    Joined:
    Jan 10, 2017
    Posts:
    21
    It's in the post.
     
  6. Marikc

    Marikc

    Joined:
    Jan 10, 2017
    Posts:
    21
    Fixed. Found that you can create an empty GameObject and place the projectile as a child then create a prefab from that with the correct rotation.

    I wish you could simply change the direction of the x, y or z on an object without rotating the object.