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

rotate a GameObject using its Transform along the y axis

Discussion in '2D' started by argens, Oct 9, 2019.

  1. argens

    argens

    Joined:
    Jun 21, 2018
    Posts:
    26
    Hello all,

    EDIT:
    and I know, i misspelled the word "muzzle" :/.

    am doing the Brackeys 2D Platformer tutorial and it is a bit outdated but thats fine, becouse it is a bit more challenging. Netherless I need help on one of the issues, that I cannot solve.

    I do a 2D platform shooter and am adding now a flash effect to the muzzle, but can't rotate it in the right way.

    The thing is, that my character shoots along the y axis (gameObject.transform.up). I have turned the axis of the muzzle in the scene view so, that it would face the right/shooting direction.
    Now my flash effect shows up in the x-axis direction and I don't know, how to tun it.

    Here is some of my code.

    Character controller script for flipping the character:

    Code (CSharp):
    1.         if (myRigidbody2D.velocity.x < 0)
    2.         {
    3.             //transform.Rotate(0f, 180f, 0f);
    4.             transform.localScale = new Vector3(-1f, 1f, 1f);
    5.         }
    6.  
    7.         else if (myRigidbody2D.velocity.x > 0)
    8.         {
    9.             //transform.Rotate(0f, 180f, 0f);
    10.             transform.localScale = new Vector3(1f, 1f, 1f);
    11.         }
    Code for shooting:

    Code (CSharp):
    1.  IEnumerator Shoot()
    2.     {
    3.         Vector2 muzlePos = new Vector2(muzle.transform.position.x, muzle.transform.position.y);
    4.  
    5.         RaycastHit2D hitInfo = Physics2D.Raycast(muzlePos, muzle.transform.up, 100f, whatToHit);
    6.         Debug.LogError("PifPaf!");
    7.  
    8.         if (hitInfo)
    9.         {
    10.             EnemyBehaviour enemy = hitInfo.transform.GetComponent<EnemyBehaviour>();
    11.  
    12.             if(enemy != null)
    13.             {
    14.                 enemy.TakeDamage(damage);
    15.             }
    16.  
    17.             Instantiate(hitEffect, hitInfo.point, Quaternion.identity);
    18.  
    19.             lineRenderer.SetPosition(0, muzle.position);
    20.             lineRenderer.SetPosition(1, hitInfo.point);
    21.         }
    22.  
    23.         else
    24.         {
    25.             Debug.DrawRay(muzlePos, muzle.transform.up * 100f, Color.yellow);
    26.             lineRenderer.SetPosition(0, muzle.position);
    27.             lineRenderer.SetPosition(1, muzle.transform.up * 100f);
    28.         }
    29.  
    30.         lineRenderer.enabled = true;
    31.  
    32.         //wait for one frame
    33.         yield return new WaitForSeconds(0.02f);
    34.  
    35.         lineRenderer.enabled = false;
    36.     }
    37.  
    38.     void WeaponFXEffects()
    39.     {
    40.         Transform cloneMuzleFlash = (Transform)Instantiate(MuzleFlashPrefab, muzle.position, muzle.rotation);
    41.         //cloneMuzleFlash.parent = muzle;                 // <- muzzle flash not appearing after this line is in the code. After erase, the flash shows only in the right direction
    42.         float size = Random.Range(1.6f, 1.9f);
    43.         cloneMuzleFlash.localScale = new Vector3(size, size / 2, size);
    44.         Destroy(cloneMuzleFlash.gameObject, 0.02f);
    45.  
    46.         //Instantiate(weaponSmoke, transform.position, transform.rotation);
    47.     }
     
    Last edited: Oct 9, 2019
  2. argens

    argens

    Joined:
    Jun 21, 2018
    Posts:
    26
    Anyone?
     
  3. Cenphon

    Cenphon

    Joined:
    Jul 18, 2019
    Posts:
    6
    Maybe you could use transform.RotateAround(transform.position, axis to rotate around (probably transform.up, degrees (probably 90 or -90))

    But I'm not sure. Could you specify your problem a little?
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
  5. argens

    argens

    Joined:
    Jun 21, 2018
    Posts:
    26
    Well, it aint working. Tried this:

    Transform cloneMuzleFlash = (Transform) Instantiate(muzleFlashPrefab, muzle.position, muzle.RotateAround(muzle.position, muzle.rotation, 90));

    but the muzle.rotation is a Quaternion, so I need to think a way around it :/.
    Anyway thanks for an idea! :)
     
  6. argens

    argens

    Joined:
    Jun 21, 2018
    Posts:
    26
    Thanks for the hint. Tried it with the following solution:
    Code (CSharp):
    1. Vector2 muzleRot = muzle.rotation.eulerAngles;
    2.         muzleRot = new Vector2(muzle.rotation.x, muzle.rotation.y + 90);
    3.         Transform cloneMuzleFlash = (Transform) Instantiate(muzleFlashPrefab, muzle.position, Quaternion.Euler(muzleRot));
    as from https://answers.unity.com/questions/476128/how-to-change-quaternion-by-180-degrees.html

    But unfortunatly it is not working :/.
     
    Last edited: Oct 10, 2019
  7. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I don't have time tonight but tomorrow I'll throw some code together for you to try.
    I'm suspecting your issue is a derivative of being a child of the player. Try creating an empty object, reset the empties transform to 0,0,0. Add the player to the empty, then pull the muzzle flash out of the player and make it a separate child of the empty.

    You may only need to make the muzzle a public or serialize private variable (unless you hate yellow errors, then tag it and find the muzzle on start() ) and then try the code you currently have. If that fails, try mine. If all of that fails, I should be back tomorrow to help.

    But I'm about 80% positive it being a child object of the player is the issue here.
     
    vakabaka likes this.
  8. argens

    argens

    Joined:
    Jun 21, 2018
    Posts:
    26
    I must tell, that you are really good. Out of nothing knowing, that I made a child of and so on. Nice one! ;-)

    Well, have tried it, but I lack of experience. I have put the animated character into an empty player object (otherwise the animation would not work).
    About the muzzle. The flash is a prefab, so it's not a child of the weapon (weapon: imgbin-american...).. Will add two pictures. One with the character (pluma) prefab and one with the weapon prefab. What I have also noticed is, that for example in the weapon prefab (imgbin-american...) a Line object, even if it seems to be a child of the weapon, it is in the character prefab. Picture001.jpg Picture002.jpg
     
  9. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    If your muzzle spawn is a child of the player itself, the rotation will not work like you want it to.

    you need an Empty GameObject (reset it's Transform)
    Perhaps you name it "Player Content"
    You want your player game object to be a child of this.
    You want all children of your player to become a child of the empty, not the player.(At least the ones you plan on rotating)
    You can still move your player but just keep in mind your empty game object may not follow so you want to set the empties transform.position to equal that of the player so it follows along.

    Now that your muzzle spawn isn't a child of the player you should be able to use rotation without issues. The child objects often take on the properties of the parent object which is what I believe your issue is here.
     
  10. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    And if your script is on the parent onject for your player animation, you may need to access your script component with GetComponentInChildren<YourAnimationScriptNameHere>()

    Code (CSharp):
    1. public GameObject yourEmptyObj;
    2. private int yourPlayersChildIndex = 0; // You want to make this int whichever child it is of the empty
    3.  
    4. // When you need to access your script inside of your function
    5.  
    6. yourEmptyObj.transform.GetComponentInChildren<AnimationScriptNameHere>(yourPlayersChildIndex)
    7. //This should access the animation script on the player child object
    8. // I set the int to 0 so your player would need to be the first child of the empty.
    So your animations should still work.
     
  11. argens

    argens

    Joined:
    Jun 21, 2018
    Posts:
    26
    Will edit this post later on.

    First wanted to thank you for your help, I do really appreaciate your time and effort, thanks a lot! Secondly congrats on your 500 posts! :)
     
  12. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I am glad to help you, no problem at all, amigo :)
    Wow, 500 post! I didn't even realize lol
    Most were probably me asking N00bie questions trying to learn to code. I'm not ashamed about it. ;)
    I know what its like to be stuck and need help so I try to do the best I can to be there when I can. I do work two full time jobs, try to design games, and starting to do YouTube tutorials between. I've spent 18-20 hours a day for the past year just working.I took 3 vacations, but used them to code 12-14hrs a day lol
    Next year, maybe I'll take a break? For now, it's crunch time! :)