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 );
}
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
}
@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)
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.