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

How could I Globally Rotate across two axis and not affect the other (Y)axis?

Discussion in 'Scripting' started by Karsnen_2, Aug 28, 2012.

  1. Karsnen_2

    Karsnen_2

    Joined:
    Nov 28, 2011
    Posts:
    89
    SCENARIO:
    My Character is falling from towards the ground and I control using the iPad. When the iPad is tilted beyond a threshold from the default position the Character rotates in accordance to the angle of tilt and an event is triggered due to the input. The game is played across X-Z axis. When the Character does not tilt beyond the threshold limit, the Character just keeps falling down due to the forces of gravity and it suffers rotation along Y-axis due to certain obstacles on the way.


    PROBLEM:
    Now when the player has the tendency to regain control, which is again tilting - the character should tilt along X or Z-axis in respective to the tilt and should maintain the last know Y-axis rotation (before the player crossed the threshold). But the code I have, the problem is the character tilts to its local position and not global.
    Which I mean to say is the character should rotate parallel to the iPad. As I am tilting the iPad, I think the input has to be in such a way that it calculates the angle and there is transform of angle from it's current rotation to the required.

    CODE:
    Code (csharp):
    1. if(MOVE) // PLAY mode
    2.         {
    3.            
    4.             timevar = Time.deltaTime * 60;
    5.             anglcal(); // Calculates the angle from accelerometer
    6.             targetRotation = Quaternion.Euler(reqAngle.x, this.transform.eulerAngles.y, reqAngle.z);
    7.             transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime*100f);
    8. }
    9.    

    REQUEST:
    I have been hitting my head against this for more than a month. I think this is a very simple problem but I just lost creativity during the process. Could you guys help me, please?

    SCREENSHOT:
    The Character after suffering a little Y axis rotation


    I just need to rotate the object depending upon the accelerometer of the iPad but not affect the rotation across the Y-axis. Or should I say, the input I get should only affect X or Z-axis and should not change or constraint Y-axis. It should also affect globally and not local.

    I hope I was clear in explaining my situation, if not kindly let me know. I would like to clear the unclear words. If you guys suggest any other solution apart from what I have, kindly let me know.


    I have set of screenshots. I hope it helps to clear what I am trying to say.

    Normal position - When there is eulerangles are zero across X, Y and Z http://postimage.org/image/f1b46v7k1/

    When the game object suffers rotation across Y axis. http://postimage.org/image/nojq74w01/

    Expected GLOBAL rotation http://postimage.org/image/dsin7hq81/

    How it actually rotates - LOCAL http://postimage.org/image/8js7ncx6p/

    Thank you.

    THANK YOU…



    Reference to Answers Section : http://answers.unity3d.com/questions/309927/how-to-rotate-an-object-around-the-world-axis-but.html
     
    Last edited: Aug 29, 2012