- Home /
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!
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".
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)"
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.
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
Follow this Question
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