Help! - Operator '' cannot be applied to operands of type 'bool' and 'float'...

I’m getting this error at line 68, Operator ‘’ cannot be applied to operands of type ‘bool’ and ‘float’, with the below code. Anyone have an ideas where I’m going wrong?

//----------------------Player move left and right-----------------------
		
		if(characterController.isGrounded  Input.GetAxis("joystick button 8"))
		{
		float walkSpeed = Input.GetAxis("Vertical") * run_Speed;
		}
		else
		{   
		float walkSpeed = Input.GetAxis("Vertical") * walk_Speed;
			
		
		
		
		
		
		//Our forward speed comes from the Vertical axis.
		//float walkSpeed = Input.GetAxis("Vertical") * walk_Speed;
			
		//Our side speed comes from our Horizontal axis.
		float sideSpeed = Input.GetAxis("Horizontal") * side_Speed;
		
		//And then use the above to create a Vector3 called 'speed' with x,y,z...
		Vector3 speed = new Vector3( sideSpeed, verticalVelocity, walkSpeed );
	
		//Our current move direction is our rotation multiplied by our move direction. This makes sure our character is moveing in the right direction no matter the rotation.
		speed = transform.rotation * speed;
		
		
		
		
		//Gravity
		
		verticalVelocity += Physics.gravity.y * Time.deltaTime;
		characterController.Move( speed * Time.deltaTime );
		}

There is no line 68

Another way is to use Input.GetKeyDown

if(characterController.isGrounded  Input.GetKeyDown(KeyCode.JoystickButton8))

Input.GetAxis(“joystick button 8”) returns a float between -1 and 1.

if (Input.GetAxis("joystick button 8")) {
    // This wouldn't work either
}

You need to tell it what you want.

if (characterController.isGrounded  !Mathf.Approximately (Input.GetAxis("joystick button 8"), 0)) {
    // This is what I believe you were going for, checking if the input is anything but 0
}
    • * * if(characterController.isGrounded == true Input.GetButton("joystick button 8") == true)
      or
    • * * if(characterController.isGrounded == true Input.GetAxis("joystick button 8") == 0f)

GetAxis returns a float and they cant be compared to bools
You were essentially writing 1f == true

Thanks for all the replys guys.

@ThermalFusion
I used your second suggestion and was able to run the game. However, my movement speed doesnt change. Do you think this is somthing you can help me with?

It seems that the character is moving at my run speed and there isnt any change when I press down on the stick.

Ive added a new input called sprint in the input options using joystick button 8 and tweaked my code…

//----------------------Player move left and right-----------------------
        
        if(characterController.isGrounded = true  Input.GetAxis("Sprint") = 0f)
        {
        float walkSpeed = Input.GetAxis("Vertical") * run_Speed;
        }
        else
        {   
        float walkSpeed = Input.GetAxis("Vertical") * walk_Speed;
            
        
        //Our side speed comes from our Horizontal axis.
        float sideSpeed = Input.GetAxis("Horizontal") * side_Speed;
        
        //And then use the above to create a Vector3 called 'speed' with x,y,z...
        Vector3 speed = new Vector3( sideSpeed, verticalVelocity, walkSpeed );
    
        //Our current move direction is our rotation multiplied by our move direction. This makes sure our character is moveing in the right direction no matter the rotation.
        speed = transform.rotation * speed;
        
       //Gravity
        
        verticalVelocity += Physics.gravity.y * Time.deltaTime;
        characterController.Move( speed * Time.deltaTime );
        }

I get no errors but one warning that my variable walkSpeed isnt being used (im at work so sorry for not being as accurate as possible with the error)

Thanks.

sorry to bpmbard you but I missed the == from the first line, this wasnt missed in my code.

I’m pretty sure you mean sprint to be a button and not an axis.
So use Input.GetKey instead (returns bool)
You seem to miss a } after the else case too. You also need to declare walkSpeed before the if statement

Edit: I did mean Input.GetButton, not key, because it works with the unity defined inputs.

@ThermalFusion

Again thanks for the reply, you’ve been a really big help :slight_smile:

I understand changing GetAxis to GetButton buthow do I declare walkSpeed before the if?

I presumed id already declared walkSpeed in…

Vector3 speed = new Vector3( sideSpeed, verticalVelocity, walkSpeed );

Thanks.

@ThermalFusion

Forget my last post you are absolutely right!

It now works exactly as I want.

float walkSpeed;
		
		if(characterController.isGrounded == true  Input.GetButton("Sprint") == true)
		{
		walkSpeed = Input.GetAxis("Vertical") * run_Speed;
		}
		else
		{   
		walkSpeed = Input.GetAxis("Vertical") * walk_Speed;
		}