Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug Setting rotation for Instantiate does nothing

Discussion in 'Scripting' started by Orwaleyh, May 18, 2024.

  1. Orwaleyh

    Orwaleyh

    Joined:
    Feb 24, 2024
    Posts:
    2
    Hi

    Im trying to Instantiate an arrow when firing a bow. But even after changing the rotation parameter in the Instantiate function it does not rotate.

    Code (CSharp):
    1. private void FireWeapon(){
    2.         //insantiate the bullet
    3.         GameObject bullet = Instantiate(bulletPrefab,
    4.         bulletSpawn.position,
    5.         Quaternion.Euler(90,0f,0f)
    6.         );
    7.         //shoot the bullet
    8.         bullet.GetComponent<Rigidbody>().isKinematic = true;
    I added the isKinematic in order for me to see how the arrow spawns before it flies away.

    But no matter which Quaternion.Euler i give the arrow instantiates vertically, instead of horizontally.
    I provide images with the results for the code example quaternion and quaternion.identity. I also attach the prefab Im using with the rotation I'd like the arrow to spawn in.

    Thanks
     

    Attached Files:

  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Likely because of the Rigidbody which takes control over the transform. The body's rotation is 0 to start with and next time physics updates it will apply that rotation to the transform.rotation, overriding your instantiate rotation (which is applied to transform).

    Try setting the rotation on the Rigidbody instead after instantiating the object.
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,189
    You shouldn't control the orientation of a bullet through code in the first place. Make sure your prefab is already setup correctly so the bullet points in the "blue" direction. If the imported model is wrongly oriented, create an empty gameobject, parent your model to that empty gameobject (make sure it's centered at 0,0,0) and rotate the model inside the empty GO so it faces the right direction. Now create a prefab out of that empty GO. Also note that thie empty GO is now your bullet. So the rigidbody component should be attached to this object, not the model which is now a child. You want to move the new parent, not the child.

    Common issues which things "rotate themselfs" are that you actually imported a model with an animation attached and that animation may overwrite the orientation. So make sure you don't have any animation on your bullet object.
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,319
    This won't happen, the Unity Transform informs the Rigidbody and vice versa.

    Additionally, ensure you don't have any lingering scripts you forgot about that are overriding the rotation.
     
    Bunny83 likes this.
  5. Orwaleyh

    Orwaleyh

    Joined:
    Feb 24, 2024
    Posts:
    2
    Hi this somewhat solved it. Now the arrows do appear to be facing front but they still wont listen to the instantiate rotation given.
    For example when instantiating with
    Code (CSharp):
    1. Quaternion.Euler(Random.Range(0, 360),Random.Range(0, 260),Random.Range(0, 260))
    The instantiated images keep aiming at the front. I checked for animations and the model has no animations that I know of, so it isn't that. I guess it must this
    But after reviewing all the script files I cant find where its happening.

    Still this results should be good enough for what Im trying to do. Thanks!
     

    Attached Files: