Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How can I change the rotation of an instantiated object?

Discussion in 'Scripting' started by jleven22, Dec 9, 2022.

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    This is for a 2D orthographic project (top down).

    I'm trying to instantiate a bullet from a gun.

    Then when the health is at max, I want to instantiate a bullet in the opposite direction also.

    For some reason, this current script is only instantiating a second bullet to the right.

    Code (CSharp):
    1.                    
    2. if (PlayerHealthController.instance.healthAtMax && LevelManager.instance.hasSwordShotA)
    3.                    
    4. {
    5.                        
    6. Instantiate(swordToFire, firePoint.position, firePoint.rotation);
    7.  
    8.                        
    9. if (LevelManager.instance.hasSwordShotAUpgrade)
    10. {
    11.  
    12.  Instantiate(swordToFire, firePoint.position, Quaternion.Euler(firePoint.rotation.x, firePoint.rotation.y, firePoint.rotation.z));
    13.  
    14.  
    15. }
    16. }
    Obviously I'm misunderstanding how a quaternion.euler is supposed to be read, but I need a little guidance. Can anyone advise? Thanks!
     
  2. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Bumping ^
    Thx!
     
  3. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    variable A = Instantiate(YOUROBJ);
    A.rotation = YOURROTATION.
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,646
    Do you have a picture of what "instantiating a second bullet to the right" means? My guess is that your bullets pushed each other outside of their collision boxes, but it's hard to tell from the description.
     
  5. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Sure! Here's an example:

    https://imgur.com/a/NRN7QnD

    You can see how the second bullet, one from this:

    Code (CSharp):
    1.  Instantiate(swordToFire, firePoint.position, Quaternion.Euler(firePoint.rotation.x, firePoint.rotation.y, firePoint.rotation.z));
    2.  
    is going only to the right.
     
  6. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Yeah but how do I write that rotation for "YOURROTATION"? Wouldn't it be the same issue? I need to figure out how to instantiate and changing only one axis angle.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,607
    So by this I guess you mean you want to shoot one backwards?

    I believe you can just do
    Quaternion.Inverse
    on your
    firePoint.rotation
    .

    EDIT: the above might flip the sprite upside down too so that may not be wanted. In that case you can use your firePoint.forward, reverse it and use look rotation:
    Code (CSharp):
    1. Vector3 backwardsDirection = -firePoint.forward;
    2. Quaternion reverseRotation = Quaternion.LookRotation(forward: backwardsDirection, upwards: Vector3.forward); //assuming forward is towards the screen
    3. Instantiate(swordToFire, firePoint.position, reverseRotation);
     
    Last edited: Dec 11, 2022
  8. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,364
    Some basics about Transform. After reading this, you might be able to pose more specific questions in a way we can help.

    https://forum.unity.com/threads/what-is-transform-forward.338384/#post-3684166

    It sounds like your project might be 2D; the same concepts apply from 3D. You can set the position AND the rotation of any object once it's been instantiated. You have to decide what position you want, and what rotation you want. There are methods on Transform, on Vector3 or Vector2, and on Quaternion classes which can calculate those values.
     
  9. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Hmm... I tried this but appear to be getting the exact same result. The new projectile instantiates only to the right.

    It might help if I share how I'm moving the bullet once it's instantiated?

    Code (CSharp):
    1.             theRB.velocity = transform.right * speed;
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,607
    I'm going to assume that 'transform' refers to the projectile's own transform, and that clearly isn't right. Get the direction you fired the original project at, and put a - in front of it. That's the direction you send the other projectile in.
     
  11. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Man I figured this out! The issue is the bullet script was in update, so the rotation was never happening quite right. So essentially I had to create a bool protected method in that script and call it while passing a bool condition. Worked it out, I'm just dumb (face palm).