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 MrCodex · Jun 16, 2013 at 05:30 PM · c#rotationtransformvector3

Problem with C# "if" statement?

Hi! I am currently creating a game wherein my character rotates 90/-90 degrees everytime I hit the corresponding button. Here are some parts of the code:

         //    going towards Z-axis
         if(lookVector.z == 1 || lookVector.z == -1)
         {
             planePosition.x = mainCamera.transform.position.x;
             planePosition.z += planeObject.renderer.bounds.size.z * lookVector.z;
             print ("Z: " + planePosition.z);
         }
         
         // going towards X-axis
         else if(lookVector.x == 1.0 || lookVector.x == -1.0)
         {
             planePosition.z = mainCamera.transform.position.z;
             planePosition.x += planeObject.renderer.bounds.size.z * lookVector.x;
             print ("x: " + planePosition.x);
         }
         
         else
         {
             Debug.Log ("hey " + lookVector);
         }

My character's "transform.forward" starts out as (0,0,1.0) and since I rotate him 90 degrees everytime, then either the X or Z is bound to be 1 or -1, if I am not mistaken?

However here's the problem, I try to rotate my character thrice: left right then left again, and the output is as follows:

 x: 475.9813
 Z: 563.7064  
 hey(-1.0,0.0,0.0)

Unity printed the "hey" part, which was not supposed to happen since my X coordinate was -1. The same happens when I rotate him in a R-L-R sequence. However there are also times when it does work normally(when I rotate him randomly). Can someone please tell my why?

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

2 Replies

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

Answer by Coderdood · Jun 16, 2013 at 05:51 PM

I suspect that the reason is because you are trying to compare floats using == . The reason this doesn't work involves dull, technical details about what floating point numbers actually are and if your interested you can read more on Wikipedia.

The use of the equality test (if (x==y) ...) requires care when dealing with floating point numbers. Even simple expressions like 0.6/0.2-3==0 will, on most computers, fail to be true[39] (in IEEE 754 double precision, for example, 0.6/0.2-3 is approximately equal to -4.44089209850063e-16). Consequently, such tests are sometimes replaced with "fuzzy" comparisons (if (abs(x-y) < epsilon) ..., where epsilon is sufficiently small and tailored to the application, such as 1.0E−13). The wisdom of doing this varies greatly, and can require numerical analysis to bound epsilon.

In order to fix it try this:

 //if(lookVector.z == 1 || lookVector.z == -1)
 //Replace with
 if ((Mathf.Approximately(lookVector.z, 1.0)) || (Mathf.Approximately(lookVector.z, -1.0)))
         
 //else if(lookVector.x == 1.0 || lookVector.x == -1.0)
 //Replace with
 else if((Mathf.Approximately(lookVector.x, 1.0)) ||(Mathf.Approximately(lookVector.x,-1.0)))

Note some other users have advised that Mathf.Approximately might not be good enough. See this answer and this answer for more details.

Comment
Add comment · Show 1 · 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 MrCodex · Jun 17, 2013 at 03:34 AM 0
Share

Ok that fixed it for now :) I'll try to use some other more reliable methods than $$anonymous$$athf.Approximately next time, but thanks a lot Coderdood!

avatar image
0

Answer by Eric5h5 · Jun 16, 2013 at 05:39 PM

If it gets to the "else" condition, that means none of the previous conditions were true. What is "lookVector"? Comparing floats to exact values is problematic. Maybe you want to round the number off before comparing.

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

16 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

Related Questions

Flip over an object (smooth transition) 3 Answers

How to change Vector3 if an objects rotation is a certain number? 1 Answer

Align a parent object model which contain fixed child alignment points to a set of fixed marker points 0 Answers

Rotating Camera around player object 0 Answers

transform.Translate moving while upside down? 1 Answer


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