Operator (less than sign) cannot be used with a left hand side of type boolean and a right hand side of type float
HI, I know that similar question have been asked many times on these forums but my instance of this is really confusing me, I have a relatively simple script where I declare a set of variables at the beginning, defining them all as float variables but when I try and compare them in an if statement, it gives me the error in the title and indicates that one of the variables is a boolean.
the errors points to (18,34), (27,34), (36,34), (45,34), (54,34) and (63,34)
#pragma strict
var acceleration : float = 1.5;
var top_speed : float = 1.5;
var speed_x : float = 0.0;
var speed_y : float = 0.0;
var speed_z : float = 0.0;
function Start () {
}
function Update () {
speed_x = speed_x - (top_speed / 2*(acceleration));
if(Input.GetKey(KeyCode.RightArrow))
{
if (-top_speed < speed_x < top_speed)
{
speed_x = speed_x + (top_speed / acceleration);
}
}
if(Input.GetKey(KeyCode.LeftArrow))
{
if (-top_speed < speed_x < top_speed)
{
speed_x = speed_x - (top_speed / acceleration);
}
}
if(Input.GetKey(KeyCode.DownArrow))
{
if (-top_speed < speed_y < top_speed)
{
speed_y = speed_y - (top_speed / acceleration);
}
}
if(Input.GetKey(KeyCode.UpArrow))
{
if (-top_speed < speed_y < top_speed)
{
speed_y = speed_y + (top_speed / acceleration);
}
}
if(Input.GetKey(KeyCode.Space))
{
if (-top_speed < speed_z < top_speed)
{
speed_z = speed_z + (top_speed / acceleration);
}
}
if(Input.GetKey(KeyCode.LeftControl))
{
if (-top_speed < speed_z < top_speed)
{
speed_z = speed_z - (top_speed / acceleration);
}
}
transform.Translate(new Vector3(speed_x * Time.deltaTime, speed_y * Time.deltaTime, speed_z * Time.deltaTime));
}
Answer by Elektrobomb · Dec 13, 2015 at 12:20 AM
Don't worry, I figured it out. for any future readers, where I went wrong was trying to do
if (-top_speed < speed_x < top_speed)
in one go because it evaluates the first inequality, declares it as a boolean and then tries to compare this boolean to top_speed, a float.
to fix it all I had to do was:
if (-top_speed < speed_x && speed_x < top_speed)