Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Serotonina · Oct 18, 2021 at 02:48 PM · rotationquaternioncomparez-axis

How to compare rotation position with an Int value (2D game)

Hello everyone,

I'm developing a series of minigames for a project, and one of them is the classic water pipe one: rotate the pipes in the correct position, water will flow, yadda yadda. The games are developed using a 2D enviroment.


The overall structure of the minigame is working correctly: each pipe get spawned at a random fixed angle degree on the z axis (0, 90, 180, 270) and when the player interact with it it can rotate the object by 90 degress each time (I'm using transform.rotation *= Quaternion.Euler). This works fine as the pipes rotate correctly.


Each pipe have an int array that, depending on the type of pipe, holds 1 or more correct angle position (I.E.: a straight pipe is positioned correctly either with a value of 0 or 180).

To define if a pipe is positioned correctly or not, I make a direct comparison between the localEulerAngles and the int array value. If the value matches, then the pipe is positioned correctly, and gets added to the number of correct pipes (this holds the number of correctly placed pipes and if its equals to the number of interactable pipes, the minigame ends).


Now, to the issue: the comparison between the two values have been quite erratic, as sometimes it works, sometimes not. Sometimes work for just one of the correct values the pipe has (I.E.: is marked as correctly placed when the value is 0, but not when 180 and viceversa), sometimes for neither of them. When a pipe does not work, I've found that by manipulating the rotation value on the inspector during runtime helps in achieving the goal, as it's resetting the value to a whole number.

Here's the code governing the pipes, nothing else happens outside of this script other than a MinigameManager that keeps track of the pipes placed correctly.

My question is: there are better ways to make a comparison between a rotation value and an Integer?


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ui_Minigame_Electricity_Button_Behaviour : MonoBehaviour
 {
 
     [SerializeField]
     int[] correctRotation;
 
     public bool isInCorrectPosition = false;
 
     int[] pieceRotations = {90, 180, 270};
     int randomRotation;
     int possibleRotations;
     GameObject button;
 
     ui_Minigame_Water_GameManager ui_Minigame_Manager;
 
     private void Start()
     {
         button = this.gameObject;
         ui_Minigame_Manager = FindObjectOfType<ui_Minigame_Water_GameManager>();
         randomRotation = Random.Range(0, pieceRotations.Length);
         button.GetComponent<Button>().transform.rotation = Quaternion.Euler(0, 0, pieceRotations[randomRotation]);
         possibleRotations = correctRotation.Length;
 
         if (possibleRotations > 1)
         {
             if (transform.eulerAngles.z.Equals(correctRotation[0]) || transform.eulerAngles.z.Equals(correctRotation[1]))
             {
                 isInCorrectPosition = true;
                 ui_Minigame_Manager.CorrectPipePosition();
                 Debug.Log(gameObject.name + " ha una rotazione di " + transform.eulerAngles.z);
             }
         }
         else
         {
             if (transform.eulerAngles.z.Equals(correctRotation[0]))
             {
                 isInCorrectPosition = true;
                 ui_Minigame_Manager.CorrectPipePosition();
                 Debug.Log(gameObject.name + " ha una rotazione di " + transform.rotation.z);
             }
         }
     }
 
     public void Rotation()
     {
         button.GetComponent<Button>().transform.rotation *= Quaternion.Euler(0, 0, 90);
 
         if (possibleRotations > 1)
         {
             if (transform.localEulerAngles.z.Equals(correctRotation[0]) || transform.localEulerAngles.z.Equals(correctRotation[1]) && !isInCorrectPosition)
             {
                 isInCorrectPosition = true;
                 ui_Minigame_Manager.CorrectPipePosition();
                 Debug.Log(gameObject.name + " ha una rotazione di " + transform.localEulerAngles.z);
             }
             else if (isInCorrectPosition)
             {
                 isInCorrectPosition = false;
                 ui_Minigame_Manager.IncorrectPipePosition();
                 Debug.Log(gameObject.name + " ha una rotazione di " + transform.localEulerAngles.z);
             }
         }
         else
         {
             if (transform.localEulerAngles.z.Equals(correctRotation[0]) && !isInCorrectPosition)
             {
                 isInCorrectPosition = true;
                 ui_Minigame_Manager.CorrectPipePosition();
                 Debug.Log(gameObject.name + " ha una rotazione di " + transform.localEulerAngles.z);
             }
             else if (isInCorrectPosition)
             {
                 isInCorrectPosition = false;
                 ui_Minigame_Manager.IncorrectPipePosition();
                 Debug.Log(gameObject.name + " ha una rotazione di " + transform.localEulerAngles.z);
             }
         }
     }
 }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by swanne · Oct 18, 2021 at 03:28 PM

Hi,
Fantastic description!
Without having a working project to test against I can only make suggestions of things I would consider.

I prefer using List instead of array. Then you could use the comparison of if list.contains(values) which I find works nicer than multiple || comparisons.

I would also test the effect of normalising your rotations to achieve a rounded value, and also build in a tolerance when comparing the values - eg, 90 = 88 - 92, 180 = 178 - 182.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Serotonina · Oct 18, 2021 at 03:51 PM 0
Share

Hi Swanne,

thank you for your answer. The issue is related to Unity conversion between Quaternions and the EulerAngles shown in the inspector. Basically it gives me the usual 90.0001 value that does not go well with the == operator. I've tried Mathf.Approximately, but it still gave me issues.


I''m leaning towards your approach for the rotations check, normalising and having a bit of leeway.

Right now I've put a simple patch that seems to work:

 if (this.transform.localEulerAngles.z > 90 && this.transform.localEulerAngles.z < 180)
         {
             transform.rotation = Quaternion.Euler(0, 0, 90);
         }



It converts the value to a fixed 90 when it's true in the start and rotation method. This way the bug seems fixed (even if it's probaly not the best way to address this).


Regarding the Array to List conversion is something I'm considering to achieve better comparisons and simplify removal of objects from the list.

Thank you again for you time, have a nice day!

avatar image swanne Serotonina · Oct 18, 2021 at 03:53 PM 0
Share

You are most welcome :)
Good luck with your game!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

175 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trouble - Limit rotation on Z-axis 0 Answers

[2D] Player's arm following the mouse 1 Answer

How do I make an angle with the X axis, Y axis, and Z axis and not rotate AROUND the axis? 0 Answers

Change the roll of the rotation 0 Answers

[Please!] Noob need's help with planet gravity movement. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges