Assets\playermovement.cs(22,28): error CS0019: Operator '<' cannot be applied to operands of type 'Vector3' and 'int'
pls help
if (isGrounded && velocity < 0)
{
velocity.y = -2f;
}
CodePudding user response:
Rather than a single number indicating how fast the object is travelling, in this case velocity
is a Vector3
containing three numbers (x, y and z values) which indicate both the speed and the direction of travel in 3D space.
If you were wanting a single number indicating the object's current speed (the length of the vector essentially), you could use velocity.magnitude
:
if (isGrounded && velocity.magnitude < 0)
{
velocity.y = -2f;
}
However that wouldn't make sense in this case as the magnitude will never be less than 0 (slower than motionless). I think what you were probably intending to do is check the object's velocity on the Y axis -- i.e. "is the object moving downwards". For that purpose, use velocity.y
for the if
statement:
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
CodePudding user response:
Unity supports only multiplication of Vector3 by a number. But you can add two vectors or subtract one from another.Thank you hope it can help you