Can't use multiple conditions in "if"

I was trying to make a counter force for my movement, but for some reason I can’t use 3 conditions in “if”

if(_grounded && Input.GetKey(KeyCode.W) == false && _playerRB.linearVelocity > Vector3.null)
        {
            _playerRB.AddForce(-transform.forward * _playerSpeed);
        }

With this I get 4 errors


But when I remove 3rd condition everything works fine

The third condition:

_playerRB.linearVelocity > Vector3.null

I don’t believe Vector3.null is a thing, nor is comparing Vector3s that way.

What are you attempting here???

2 Likes

I guess you mean Vector3.zero?

1 Like

Probably, but this answer is misleading because you can not compare vectors with “greater than”, like Kurt said. He probably want to do something like

_playerRB.linearVelocity.magnitude > 0f
or usually better to use something like
_playerRB.linearVelocity.magnitude > 0.1f
or even better (slightly more performant)
_playerRB.linearVelocity.sqrMagnitude > 0.01f

2 Likes

General advice NOT to cram multiple unrelated conditions into a single if statement. This will be hard to debug and reason about.

When using Input, I consider it best practice to react to input first and foremost, and perform any additional checks after the fact. So Input conditions should all be at the same level. And refactor any non-obvious checks to methods that “speak out loud” what they actually check.

I would translate it to this:

if (Input.GetKey(KeyCode.W) == false)
{
    if (_grounded && IsMoving)
    {
        // do stuff
    }
} 

bool IsMoving => _playerRB.linearVelocity > _threshold;

Much easier to read because that’s what 99% of programming is all about.

1 Like