Geek Culture
Published in

Geek Culture

Using a Boolean Logical Operator

Currently if a Player is flying into an item it will always collect it. But that’s not really what we want, instead we’d like the Player only to be able to pick up the item once and only when it’s allowed. So for example we don’t want to execute the health Power up logic if the Player is already full health. To fix that we could create a Boolean Logical Operator in the Player.cs Script and create a reference to it in the Item.cs Script.

Bool Type

Short for boolean, a ‘bool’ type is a data type that can only return one of two values, true or false. The bool type is the result type of comparison and equality operators and false by default. It can be used for controlling conditional expressions.

Boolean Values can only return true or false.
Boolean conditional expression

Applied to our Players.cs Script;

If our current health is equal to our max. health, we return false and else we return true.

However since the default value of the type bool is false we can shorten it down declaring; result is always false and return true if current health is less than our max. health.

Implementation

Now that we have a method to check if the Player is full health or not, we can call this method in the Item.cs Script and only allow a health power up to be picked up when the Player is not full health.

Previous | Next

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gert Coppens

Gert Coppens

Software Engineer — Unity Game and Application Developer