Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Getting rotation degrees for if statement

Discussion in 'Scripting' started by Jojoba007, May 10, 2018.

  1. Jojoba007

    Jojoba007

    Joined:
    Feb 11, 2017
    Posts:
    148
    Hi,

    I'm trying to make a method that takes care of ending the game when my character rotates more then 70 degrees in plus and minus on the X axis. I'm using the following code. But the code get's instantly activated as soon as i jump with the character.

    Anyone a suggestion on doing this the proper way?


    Code (CSharp):
    1.     void FixedUpdate ()
    2.     {
    3.         EndGameOnDegrees ();
    4.     }
    5.  
    6.  
    7. //End the game if the player gets stuck opsidedown for certain amount of seconds
    8.     public void EndGameOnDegrees ()
    9.     {
    10.         float min = -70f;
    11.         float max = 70;
    12.         float time = Time.deltaTime;
    13.  
    14.  
    15.         if (transform.rotation.eulerAngles.x < min || transform.rotation.eulerAngles.x > max && time > 4f) {
    16.  
    17.             Debug.Log ("More or less then 70 degrees on X axis");
    18.  
    19.             if (GameOverManager.instance != null) {
    20.  
    21.                 GameOverManager.instance.GameOverShowpanel ();
    22.             }
    23.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    rotations can be a little tricky as there are many combinations of the 3 euler angle components that can represent a given quaternion. You might want to look into something like Vector3.Angle (esp. if you always want the min/max to be symmetrical).

    Also, be careful with combining AND and OR, I suspect you mean

    ( ( a || b ) && t > 4f)

    on line 15 rather than what you've written which evaluates to

    ( a || ( b && t > 4f) )
     
    Last edited: May 10, 2018
    Jojoba007 likes this.
  3. Jojoba007

    Jojoba007

    Joined:
    Feb 11, 2017
    Posts:
    148
    I changed the method to the following, so far it seems to work. Thank you for pointing me in the right direction.


    Code (CSharp):
    1.  public void EndGameOnDegrees ()
    2.     {
    3.         float min = -70f;
    4.         float max = 70;
    5.         float time = Time.deltaTime;
    6.         float angle = Vector3.Angle(Vector3.up, transform.up);
    7.  
    8.  
    9.         if ((angle < min || angle > max) && time > 4f) {
    10.  
    11.  
    12.             Debug.Log ("More or less then 70 degrees on X axis");
    13.  
    14.             if (GameOverManager.instance != null) {
    15.  
    16.                 GameOverManager.instance.GameOverShowpanel ();
    17.             }