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 TheBorDr · Sep 15, 2012 at 09:28 PM · errorfloatbool

Cannot implicitly convert type 'float' to 'bool'

Okay, so I am trying to make it so that if the character is moving, play the 'run' animation, if he is not moving the 'idle' animation. This is my script:

 using UnityEngine;
 using System.Collections;
 
 public class FPSInputConC : MonoBehaviour {
 
         public float speed = 200;
             
                     public float jumpSpeed = 100;
     
                                 public Vector3 MoveDirection = Vector3.zero;
     
                                         public float gravity  = 90;
     
                                                 public bool grounded = false;
                                                         
                                                         public bool moving = false;
     
     // Update is called once per frame
     void Update ()
     {
         
         
         if (grounded) {
         
             MoveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
             
             MoveDirection = transform.TransformDirection (MoveDirection);
             
             MoveDirection *= speed;
         }
         if  (moving == true){
             animation.Play("run");
         
         }
         
         if(grounded){
 
             if(Input.GetKey(KeyCode.Space)){
             
                MoveDirection.y = jumpSpeed;
                 
             }
 
             if(MoveDirection[0] = 0){
                 moving = true;
 
             }
 
         }
         
         MoveDirection.y -= gravity * Time.deltaTime;
             
                     var Controller = GetComponent<CharacterController>();
         
                                 var flags = Controller.Move(MoveDirection * Time.deltaTime);
         
                                                 grounded = (flags & CollisionFlags.CollidedBelow) !=0;
         
     }
     
     
     
 }


The error is at line 45, this script is a modified version of another. Please, if anyone has the time, help. Thank you!

Comment
Add comment · Show 1
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 Graham-Dunnett ♦♦ · Sep 15, 2012 at 09:32 PM 1
Share

which line is line 45?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by mhughson · Sep 15, 2012 at 11:18 PM

 if(MoveDirection[0] = 0){
     moving = true;
 }

This line will be an issue. You are using the assignment operator (=), instead of comparison (==).


Long term you can get into the habit of putting constants first in comparisons.

 if( 0 == MoveDirection[0] )

That way if you accidentally use the assignment operator it will give you a compiler error since it is illegal to try and assign a value to "0".

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
1

Answer by dcAlge · Sep 15, 2012 at 11:18 PM

Your problem is here: if(MoveDirection[0] = 0)

MoveDirection[0] = 0 assigns the value 0 to MoveDirection[0], it is NOT a comparision with a bool as a result.

What you want is "if(MoveDirection[0] == 0)"

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 Ludus · Sep 15, 2012 at 10:09 PM

I think your issue is here.

 >   grounded = (flags & CollisionFlags.CollidedBelow) !=0;


Your error message "Cannot implicitly convert type 'float' to 'bool'" is saying your trying to convert a boolean (true/false) into a float (number). Up top you declared grounded as false, and later your checking it against a number, which it can't do. Change that. I think.

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 Eric5h5 · Sep 15, 2012 at 10:27 PM 0
Share

No, there isn't a float on that line, and the expression

 (flags & CollisionFlags.CollidedBelow) != 0

is either true or false, so nothing is being converted.

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

14 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

Related Questions

Convert type `float' to `bool', Load 1 Answer

Transform rotation 180° becomes -5.008957e-06? 2 Answers

What is wrong with my throttle script? 2 Answers

Operator '+' cannot be used 1 Answer

error CS0266: Cannot implicitly convert type `double' to `float'. An explicit conversion exists (are you missing a cast?) whats wrong? 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