Search Unity

error CS0029: Cannot implicitly convert type 'float' to 'bool'

Discussion in 'Scripting' started by duanegra, Oct 9, 2018.

  1. duanegra

    duanegra

    Joined:
    Aug 28, 2018
    Posts:
    10
    I created some 2D platform character behaviors in a simple scene that worked fine when using the up,down, left, right arrow keys on my keyboard. The player moves horizontally, then when a ladder is detected, the player can move vertically up and down the ladder. Raycast is used to detect the ladder, then gravity scale is set to zero as the player begins to move up the ladder. Gravity scale is set back to 5 when the player is no longer on the ladder.

    The problem occurs when I substitute a virtual joystick for mobile device control. I am using the "Joystick Pack" from the Asset store. I have been able to successfully swap keyboard axis controls, with the joystick controls in other scripts. But for some reason this particular script show the following error in the consul:

    "error CS0029: Cannot implicitly convert type 'float' to 'bool' " 


    The error is on line 34. In comments on the same line, I put the code for the keyboard controls, which worked fine.

    I spent a few hours trying to resolve this error, and searching online for possible solutions.

    I appreciate any assistance with this.


    Here is the full C# script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player_Climber_m : MonoBehaviour {
    6.  
    7.     // move variables
    8.     private float inputHorizontal;
    9.     private float inputVertical;
    10.     private Rigidbody2D rb;
    11.     public float speed;
    12.  
    13.     //Ladder variables
    14.     public float distance;
    15.     public float climbSpeed;
    16.     private bool isClimbing;
    17.     public LayerMask whatIsLadder;
    18.  
    19.     public Joystick joystick;
    20.  
    21.     void Start(){
    22.         rb = GetComponent<Rigidbody2D> ();
    23.     }
    24.  
    25.     void FixedUpdate(){
    26.  
    27.         inputHorizontal = joystick.Horizontal; //Input.GetAxis("Horizontal");      
    28.         rb.velocity = new Vector2 (inputHorizontal * speed, rb.velocity.y);
    29.  
    30.         RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector2.up, distance, whatIsLadder);
    31.         //joystick.Vertical
    32.  
    33.         if (hitInfo.collider != null) {
    34.             if (joystick.Vertical) {   // if (Input.GetKeyDown (KeyCode.UpArrow)) {      
    35.                 isClimbing = true;
    36.             }
    37.  
    38.         } else {
    39.             isClimbing = false;
    40.         }
    41.  
    42.         if (isClimbing == true) {
    43.             inputVertical = Input.GetAxis ("Vertical");
    44.             rb.velocity = new Vector2 (rb.velocity.x, inputVertical * climbSpeed);
    45.             rb.gravityScale = 0;
    46.         } else {
    47.             rb.gravityScale = 5;
    48.         }
    49.  
    50.     }
    51.  
    52. }
    53.  
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    It would seem that Joystick.horizontal Returns a float (see line 27), so your test if (joystick.vertical) doesn't make sense.

    Try

    if (joystick.vertical > 0f)

    instead.
     
  3. duanegra

    duanegra

    Joined:
    Aug 28, 2018
    Posts:
    10
    Awesome! That worked perfectly. I just changed > to != so it will work for up or down.

    Thanks, and all the best.