Search Unity

[SELF-ANSWERED] 2D Z axis rotation - need to clamp it -90 to 90 degrees

Discussion in 'Scripting' started by Nicholas1010, Jun 11, 2017.

  1. Nicholas1010

    Nicholas1010

    Joined:
    Aug 2, 2014
    Posts:
    20
    I am working on a 2D top-down shooter, and I ran into problem I've been confused over all day yesterday.

    So I'll start off with a video I recorded:



    So what do you want to do? I have character changing sprites every time it changes direction (WASD) and I want character to aim his weapon at mouse position, but I want to clamp it so it does not rotate all the way around, just the direction its facing, easy - use mathf.clamp, but I've ran into really annoying problem, which I'll explain below.

    What do you want to show in that video? Please pay attention to Z axis as I rotate weapon with mouse, Z axis suddenly turns negative after 180 degrees, now I am pretty bad at this kind of math so sorry if I am asking something pretty stupid, but why?

    Whats the problem though?
    Well, I cannot find an easy way to clamp it correctly if it suddenly turns negative for reason I don't get. :(

    I'll try to show parts of my code now, its extremely messy though, yesterday I tried everything I could ever think of so...

    The mouse-rotation part:

    Code (csharp):
    1.  
    2. Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    3. mousePos = mousePos - cw.weaponHoldPosition.transform.localPosition;
    4. angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    5.  
    6. cw.weaponHoldPosition.eulerAngles = new Vector3(0, 0, angle);
    7.  
    I honestly remember using some different method which gave similar results before, but after digging through google for hours I ended up using this.

    Part of clamping in case if last direction is on the right facing side (Which in fact works I believe, its just other sides that are messed up due to me not understanding how these kind of angles work):

    Code (csharp):
    1.  
    2. angle = Mathf.Clamp(angle, -90, 90);
    3.  
    So, can I please get some suggestions on how to fix my problem? What can I do to get this working? Also, if I left out something or explained something poorly, let me know, I will correct it.

    Thanks for reading!
     
  2. Nicholas1010

    Nicholas1010

    Joined:
    Aug 2, 2014
    Posts:
    20
    Alright then. Even though I tried things VERY similar things to this yesterday, I didn't realize I could simply do this:

    Code (csharp):
    1.  
    2.         if (cw.weaponHoldPosition.transform.eulerAngles.z > 90 && cw.weaponHoldPosition.transform.eulerAngles.z < 270)
    3.         {
    4.             cw.weaponHoldPosition.GetComponentInChildren<SpriteRenderer>().flipY = true;
    5.         }
    6.         else
    7.         {
    8.             cw.weaponHoldPosition.GetComponentInChildren<SpriteRenderer>().flipY = false;
    9.         }
    10.  
    It flips the weapon so its not upside down and allows you to do 360 weapon rotation, but it actually looks normal this time.

    Guess sometimes you gotta stop being nervous, I could had fixed this yesterday if I would stay calm. :D