Search Unity

Unity 2D Euler Rotation when X Scaled to -1??

Discussion in '2D' started by CStunner, Jan 22, 2015.

  1. CStunner

    CStunner

    Joined:
    Jul 12, 2013
    Posts:
    39
    1. I have a character, it points the gun (a scaled down Quad at the time) at the Mouse position, it's working fine alright, but when i x scale the Player (parent GameObject) to -1 to Flip it, it doesn't work normal, i mean it flips but then the Rotation of the Gun (child gameObject) is disturbed..

    Here's the rotation code:

    Code (CSharp):
    1. myTransform = transform;
    2.  
    3. Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4.         float diffX = mouseWorldPos.x - myTransform.position.x;
    5.         float diffY = mouseWorldPos.y - myTransform.position.y;
    6.         float angle = Mathf.Atan2(diffY, diffX) * Mathf.Rad2Deg;
    7.         Vector3 targetEuler = Vector3.zero;
    8.         targetEuler.z = angle;
    9.      
    10.         myTransform.rotation = Quaternion.Euler (targetEuler);
    Here's the rotation without the Flip:



    And here's the rotation After the Flip:



    The projectile is instantiated with transform.rotation as the 3rd argument.

    How to deal with this? I tried adding offset value, but it didn't work..

    2. When using Lerp in Update, is it always going to be frame independent? I mean if i put 0.1 as 3rd argument, can i rest assured that the Lerping won't be affected by the Frames per second?

    Thanks
     
    Last edited: Jan 22, 2015
  2. DustyMcp

    DustyMcp

    Joined:
    May 23, 2013
    Posts:
    25
    I belive your angle is wrong, it goes from -180 to 180

    Code (CSharp):
    1. myTransform = transform;
    2. Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    3.         float diffX = mouseWorldPos.x - myTransform.position.x;
    4.         float diffY = mouseWorldPos.y - myTransform.position.y;
    5.         float angle = Mathf.Atan2(diffY, diffX) * Mathf.Rad2Deg;
    6.         Vector3 targetEuler = Vector3.zero;
    7.         targetEuler.z = angle;
    8.       if (angle < 0)
    9.                  angle += 360;
    10.         myTransform.rotation = Quaternion.Euler (targetEuler);
    Try this out should be abit easier calculating im not at a unity machine atm so not sure if this is correct, but its a step in the right direction.
     
  3. CStunner

    CStunner

    Joined:
    Jul 12, 2013
    Posts:
    39
    Thank you, right or not, i really appreciate your reply..

    I tried it and it still does not work, i'm quite bad at maths, besides i got that code from someone else's thread to be honest



    I don't know if i'm right, but looking at this, i'm assuming that doing something like an X-scale change to the opposite axis messes rotation up..

    I looked at the Unity 2D Sample code, it simply instantiates the Projectile with Quaternion.Euler (new Vector3(0, 0, 180)) rotation to set it's direction to the Left, no mouse aim thing that i want. IDK what to do in my case ..
     
  4. CStunner

    CStunner

    Joined:
    Jul 12, 2013
    Posts:
    39
    no one? Am i the only one who has this kind of problem?
     
  5. Whalepork

    Whalepork

    Joined:
    Sep 4, 2013
    Posts:
    18
    Have you tried using transform.localRotation? I had a similar problem and using the transform.localRotation solved the issue.
     
  6. CStunner

    CStunner

    Joined:
    Jul 12, 2013
    Posts:
    39
    Thank you for the reply Whalepork, unfortunately it didn't work ..
     
  7. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Why don't you just add 180 to the angle when it is flipped. That should do the trick.
     
    CStunner likes this.
  8. CStunner

    CStunner

    Joined:
    Jul 12, 2013
    Posts:
    39
    Thank you, i decided to drop this idea from the design, adding offset value didn't work, and i did use the Gun as a separate GameObject, but now i can't Clamp it down to any range of values, because sometimes the range works, sometimes it doesn't maybe it's because i am using a code which uses the gameobject's transform position?

    Anyway, it wasted too much time of mine, literally days of trial and error, and still not satisfying result, so i decided to drop this idea and go with a simple Left, Right shooting, without the Mouse Aim mumbojumbo..

    Thanks anyways..
     
  9. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    it's not a code solution but you could use mecanims mirror option - I think.
     
  10. coshea

    coshea

    Joined:
    Dec 20, 2012
    Posts:
    319
    Don't "x scale the Player (parent GameObject) to -1 to Flip it"

    instead rotate on Y axis by 180 degrees when you flip.
     
  11. CStunner

    CStunner

    Joined:
    Jul 12, 2013
    Posts:
    39
    I thank you all for your reply .. but @coshea X Scaling to -1 is the method taught by Unity in the Live Training Session, rotating the Y axis by 180 degrees makes the Quad invisible (messes the normal or something), i haven't tried it with Sprite though.

    Read on if you want to, or you can skip..

    My Objective was to prevent the Gun from going beyond certain angle, as you can't carry a rifle and shoot it by rotating your hand to 180 degrees (your back).

    After trying all the things i could imagine, i figured out that i had the wrong idea about what the angle was.. The solution was really simple.
    I had made a stupid assumption that the calculated angle variable above goes all the way from 0 to 360 and i had made a second wrong assumption that it would be the Euler Angle needed.

    Turned out, it did not, the "angle" only went from 0 to 180, the Euler Angle on the other hand goes all the way from 0 to 360.
    So i had to make the Gun gameobject separate from the Player, and in every Update, adjust it's position and x scale.

    Then, if player is facing Right, Clamp the angle variable between say -45 and 60.
    If Player is facing left:
    - tempQ = Quaternion.Euler (0,0,angle);
    - tempE = tempQ.eulerAngles;
    - if (tempE.z <= 225 && tempE.z >= 120) // can Rotate the mouse

    Then it solved my issue..
     
    theANMATOR2b likes this.