Splendex
Splendex

Reputation: 11

Trying to make a script where you cant move further down when you have reached a certain height

Using the code below, I get the following error:

Operator '<' cannot be applied to operands of type 'Vector3' and 'int'

The code:

void PlayerTurn()
{
    if(state != BattleState.PLAYERTURN) 
    {
        return;
    }

    if (Input.GetKeyDown("w")) 
    {

    }
    if (Input.GetKeyDown("s") && player.transform.position < -3)
    {
            
    }
}

What am I doing wrong?

Upvotes: 1

Views: 30

Answers (1)

Stefan
Stefan

Reputation: 17658

player.transform.position is a Vector3, a 3 dimensional vector

-3 is an int, 1 dimensional integer

< is the is less then operator.

You cannot "compare" those 2 types like that without defining a rule for it.

For example, you could take the absolute value of the vector, and compare that with an int, but that still leaves you with handling the - sign since the modulus is always positive.


Alternatively, if you're talking about a specific axis of the 3 dimensional vector, it will be something like:

//using `y` since you mentioned height.
player.transform.position.y < -3f

Upvotes: 1

Related Questions