• 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 /
This question was closed Dec 22, 2016 at 05:40 PM by Nekoluffy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Nekoluffy · Sep 13, 2014 at 12:31 PM · floattypeoperatorcannot

Operator || cannot be applied to types float and float?

Hi I keep getting an error with my code while following a tutorial on making rigidbody character controllers. Operator '||' cannot be applied to operants of type 'float' and 'float'. Im following the tutorial from the book unity 3.x scripting/cookbook. Except instead of using java, its in C#.

the java version from the tutorial:

 public var Speed : float = 5.0;
 public var MoveDirection : Vector3 = Vector3.zero;
 function Movement (){
 if (Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
 MoveDirection = Vector3(Input.GetAxisRaw("Horizontal"),MoveDirecti
 on.y, Input.GetAxisRaw("Vertical"));
 this.transform.Translate(MoveDirection);

 My version

     public float speed = 5.0f;
     public Vector3 moveDirection = Vector3.zero;
     private float h = Input.GetAxis("Horizontal");
     private float v = Input.GetAxis("Vertical");
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     void Movement()
     {
         if( Input.GetAxis("Horizontal") || Input.GetAxis ("Vertical") )
             moveDirection = Vector3( h, moveDirection.y, v);
 
             this.transform.Translate(moveDirection);
         
 
     }



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

  • Sort: 
avatar image
0
Best Answer

Answer by VesuvianPrime · Sep 13, 2014 at 12:37 PM

I'm not familiar with Java, but in Python you can use most (all?) objects as booleans, which I presume is what the book is trying to do.

For example, the following is legal in Python:

 if 0.0: # Evaluates to False
     DoSomething()
 elif 26.1: # Evaluates to True (the float isn't zero)
     DoSomethingElse()

But the following is illegal in C#:

 if (0.0f)
     DoSomething();
 else if (26.1f)
     DoSomethngElse();

C# doesn't let you cast things to booleans like that.

So, reading your code, I presume the author was simply trying to detect if there is any motion on either of the input axis.

The check you probably want to be making is:

 bool horizontal = !Mathf.Approximately(Input.GetAxis("Horizontal"), 0.0f);
 bool vertical = !Mathf.Approximately(Input.GetAxis("Vertical"), 0.0f);

 if (horizontal || vertical)
     ...
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 Nekoluffy · Sep 13, 2014 at 02:50 PM 0
Share

I see now. Thank you Very much. Its C# that doesnt let that happen. I switched it over to Java like it was intended and it seems to be working fine. I was hoping to be able to initialize GetAxis into a variable though, but JavaScript doesnt let that happen apparently. At least without any errors, but this will do. Thank you.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

error CS0019: Operator `&' cannot be applied to operands of type `float' and `float' 0 Answers

error CS0019: Operator `>' cannot be applied to operands of type `method group' and `method group' 1 Answer

[SOLVED] Receive a float from a quaternion? 1 Answer

Operator cannot be used with left hand side of type.... 1 Answer

How to compare float for equality? 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