Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

If-statement problem (float to bool error) - please provide an explanation

Discussion in 'Scripting' started by Viking1972, Apr 8, 2013.

  1. Viking1972

    Viking1972

    Joined:
    Jul 24, 2009
    Posts:
    74
    Hi

    I have problem with understanding why I get an Assets/Scripts/cubeMove.cs(32,17): error CS0029: Cannot implicitly convert type `float' to `bool'

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class cubeMove : MonoBehaviour {
    6.    
    7.     public int playerSpeed;
    8.    
    9.     public int playerTurnSpeed;
    10.    
    11.     private Transform myPlayerObject;
    12.    
    13.     // Use this for initialization
    14.     void Start () {
    15.        
    16.         playerSpeed = 5;
    17.        
    18.         playerTurnSpeed = 5;
    19.        
    20.         myPlayerObject = transform;
    21.        
    22.         myPlayerObject.position = new Vector3(2,1,2);
    23.    
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.        
    29.         myPlayerObject.Translate(Vector3.right * Input.GetAxis("Horizontal") * playerSpeed * Time.deltaTime);
    30.        
    31.         myPlayerObject.Translate(Vector3.forward * Input.GetAxis("Vertical") * playerSpeed * Time.deltaTime);
    32.        
    33.         if(Input.GetAxis("Vertical"))
    34.         {
    35.             myPlayerObject.Rotate(Vector3.forward * playerTurnSpeed * Time.deltaTime);
    36.         }
    37.        
    38.        
    39.    
    40.     }
    41. }
    42.  
    43.  
    44.  

    My if-statement, where the error occurs, does the same as the one below (or is supposed to !), but they don't get an error :

    Code (csharp):
    1.  
    2.  void Update ()
    3.     {
    4.                
    5.         if(Input.GetKey(KeyCode.RightArrow))
    6.             transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
    7.     }
    8.  
    Unity's Translate Rotate tutorial

    Would someone be so and explain ?

    Also, should I stop using the Input Manager for movement and instead use: Input.GetKey(KeyCode.SomeKey
    ) ?
     
  2. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
  3. Spoink

    Spoink

    Joined:
    May 9, 2011
    Posts:
    33
    Hey hey,

    The function GetAxis returns a float (number) while the function GetKey returns a bool (true or false).
    If you want to check if the buttons in the GetAxis are down or not you need to check if the float the function return is not 0.
     
  4. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    Actually, Spoink is right, check if it's not equal to zero. Something like:
    Code (csharp):
    1.  
    2. if(Input.GetAxis("Vertical") != 0)
    3.  
     
  5. Viking1972

    Viking1972

    Joined:
    Jul 24, 2009
    Posts:
    74
    Hi guys

    Thank you for helping out on a newbie distress call.

    I have gotten the code to work by chekking to see if it was more than zero as told you me to @wolfhunter777 - thank you for not writting me the code in your first reply - in hindsight I became happy once I figured out how to check and see if the value was more than zero (I consider this a small victory for the noob/newbie)

    I had actually thought of using the not equal to (!=), however, I wasn't brave enough to take the plunge.

    Anyway, thank you both for helping a noob in need !
     
  6. TheUninvited

    TheUninvited

    Joined:
    Jun 22, 2018
    Posts:
    31
    thank you i was struggling so much i had no idea that get axis uses 1 or -1.

    I've used to create input for keyboard and android :
    https://hatebin.com/ukwcerasmu