Trouble with understanding code that uses "==" operator.

Hi there,

I’m following a tutorial on Youtube where the tutor is coding a script that detects whether the player has collided with obstacles. (I want to avoid overwhelming anyone who might read this post with information so I’ve simplified the coding that the tutor writes.)

In the script the tutor created a struct called CollisionInfo that contains four boolean variables called above,below,left and right:

public struct CollisionInfo{

	public bool above,below;
	public bool right,left;

}

He creates a reference to the struct at the top of the script by writing “CollisionInfo collisions” and then writes the following code further down the script involving an if statement which uses the “==” operators:

 if (hit) {

	
	collisions.above == directionY == 1;
	collisions.below == directionY == -1;
				
				
						
  }

The hit variable refers to a RaycastHit2D variable. Basically it is saying that if a raycast has hit an obstacle then hit is true. I’m finding it difficult to understand exactly what the two lines of code within the if statement mean? I assume that what it means is that if the directionY variable is equal to 1 then then the boolean value called “above” that resides in the CollisionInfo struct will equal to true?

I’ve looked for ages online trying to understand what the == operator means in this context but I still don’t understand.

I would appreciate if someone would be kind enough to explain what does two lines of code mean.

Kind regards

I believe that you are trying to find something that does not exist - or I have failed as a programmer.
The code is wrong. Below is the correct one


if (hit)
{
      //means that above is true if directionY is 1
      collisions.above = directionY == 1;

      //means that below is true if directionY is -1
      collisions.below = directionY == -1; 

}

So its just set’s the current state of collision, up or down

This looks like a mistake to me… likely the code is supposed to be collisions.above = directionY == 1;, which would make sense. The code as is does nothing, there is no assignment or function call.