Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 bls61793 · May 21, 2013 at 08:39 AM · c#not workingif statementsimpleeuler angles

C# Unity IF Statement not working properly and I can't figure out why. Deals with basic rotation stuff.

So, I was working on my project and making a simple IF statement that performs certain functions based up the current Z rotation (in Euler Angles) of the object that the script is attached to. But I ran into a problem, which is demonstrated in the "BulletSponge()" method below.

 void Update () {
         ConductMovement();
         BulletSponge();
         CheckForFiring();
         DecrementFiringTimer();
         Debug.Log ("Rotation: " + transform.eulerAngles);
     }
 
 
     void BulletSponge(){
         float BOB = gameObject.transform.eulerAngles.z;
         
         if (BOB == 180){
             Debug.Log ("WIN");
         }
         Debug.Log ("BOB: " + BOB);
     }


The problem is that both the "Debug.Log ("Rotation: " + transform.eulerAngles);" and "Debug.Log ("BOB: " + BOB)" say that "gameObject.transform.eulerAngles.z;" is equal to 180 in the Console Log, yet the IF statement won't fire (The "WIN" message never shows up.) Anyone have any ideas? I appreciate any help.

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

3 Replies

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

Answer by KiraSensei · May 21, 2013 at 08:56 AM

Try Mathf.Approximately()

You are comparing a float with an int. Approximations can be a problem here.

Comment
Add comment · Show 5 · 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 bls61793 · May 21, 2013 at 09:14 AM 0
Share

That Worked. Thanks a ton. I guess I don't completely understand how floats work. Why is it not exactly 180, yet my outputs give me exactly 180 or 180.0?

avatar image Fornoreason1000 · May 21, 2013 at 09:17 AM 0
Share

short answer int = 180, float = 179.999999999f but sometimes read as 180f.

also if $$anonymous$$ira's Answer helped I suggest you show that by Ticking $$anonymous$$ira's Answer

avatar image KiraSensei · May 21, 2013 at 09:18 AM 0
Share

There are always approximations with floats.

In your example, maybe your outputs are rounding the number before showing it to you.

Everytime you want to compare a float with something else (a float or an int), you need to think about approximations errors. That's the problem with an exact science :)

avatar image bls61793 · May 21, 2013 at 09:19 AM 0
Share

Thanks. $$anonymous$$ath is so weird... I have a love/hate relationship with it.

EDIT: Not letting me tick $$anonymous$$ira, says I need to login as another user. I'll figure it out. Sorry, I'm new here. The thumbs up isn't working :( I'll get it/ Thanks people :P

avatar image KiraSensei · May 21, 2013 at 10:18 AM 0
Share

If I remember correctly, you can tick the answer anytime, but you have to wait for a high enough karma to thumbs up.

avatar image
1

Answer by Fornoreason1000 · May 21, 2013 at 08:48 AM

how are you getting BOB to equal 180? are you sure it reaches Exactly 180? if the game_object is the player, you will have a very hard time getting it to equal 180 without hard coding it. try this

 if(BOB > 179 && BOB < 181)

or you can cast BOB as an int then back to a float

 BOB = (float)((int)gameObject.eulerAngles.z)
 

hope it helps :)

Comment
Add comment · 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
0

Answer by bls61793 · May 21, 2013 at 09:34 AM

I have it set exactly to 180 because that is the default rotation that my object has. The original idea is to have a set of IF statements handling 8 different degree angles caused by the following code:

 void HandlePlayerMovementandRotation(){
     //Case Statements Handling Player Movement and Rotation
         if (Input.GetKey(KeyCode.A) == true){
             transform.Translate( -4f * Time.deltaTime, 0, 0,Space.World);
             //Check if Backward Movement is Enabled
             if (CharFaceOppositeMovement == true){
                 transform.rotation = Quaternion.Euler(0,0,90);
             }
             else{
                 transform.rotation = Quaternion.Euler(0,0,-90);
             }
         }
         if (Input.GetKey(KeyCode.D) == true){
             transform.Translate( 4f * Time.deltaTime, 0, 0,Space.World);
             //Check if Backward movement is enabled
             if (CharFaceOppositeMovement ==true) {
                 transform.rotation = Quaternion.Euler(0,0,-90);
             }
             else{
                 transform.rotation = Quaternion.Euler(0,0,90);
             }
         }
         
         if (Input.GetKey(KeyCode.W) == true){
             transform.Translate( 0, 4f * Time.deltaTime, 0,Space.World);
             if (CharFaceOppositeMovement == true){
                 transform.rotation = Quaternion.Euler(0,0,0);
             }
             else{
                 transform.rotation = Quaternion.Euler(0,0,180);
             }
         }
         
         if (Input.GetKey(KeyCode.S) == true){
             transform.Translate(0, -4f * Time.deltaTime , 0,Space.World);
             if (CharFaceOppositeMovement == true){
                 transform.rotation = Quaternion.Euler(0,0,180f);
             }
             else{
                 transform.rotation = Quaternion.Euler(0,0,0);
             }
         }
     }
     
     
         void HandleAnglesDuringDiagonalMovement(){
     //Expressions Handling angles during Diagonal Movement
         if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A) == true){
             if (CharFaceOppositeMovement == true){
                 transform.rotation = Quaternion.Euler(0,0,45f);
             }
             else{
                 transform.rotation = Quaternion.Euler(0,0,-135f);
             }
         }
         if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) == true){
             if (CharFaceOppositeMovement == true){
                 transform.rotation = Quaternion.Euler(0,0,-45f);
             }
             else{
                 transform.rotation = Quaternion.Euler(0,0,135f);
             }
         }
         if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A) == true){
             if (CharFaceOppositeMovement == true){
                 transform.rotation = Quaternion.Euler(0,0,135f);
             }
             else{
                 transform.rotation = Quaternion.Euler(0,0,-45f);
             }
         }
         if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D) == true){
             if (CharFaceOppositeMovement == true){
                 transform.rotation = Quaternion.Euler(0,0,-135f);
             }
             else{
                 transform.rotation = Quaternion.Euler(0,0,45f);
             }
         }
     }


I forgot to mention, my game is 2D. My character is a sprite pasted on a thin cube mesh. The cube mesh starts at a rotation of 180*. The default rotation is 0* facing the bottom of the screen.

Comment
Add comment · 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

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

15 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

Related Questions

Really weird problem with if statements [C#] 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Simple question about targetting 0 Answers

Clamp an object on 2 sides 0 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