how do I make a C# "if" statment check for matching z cordinates between to different objects?

I am trying to make an enemy shot if it’s z coordinate is the same as the player. But I cant find out how to word the “if” statement that will check to see if they match.

Please help! C# Is the coding language I’m using.

Assuming you’ve already coded the variable that accesses the Player’s transform, you want to do something like:

if (Mathf.Abs(player.position.z - transform.position.z) < someThreshold) {
    // Do some shooting.
}

What you don’t want to do is something like this:

if (transform.position.z == player.position.z)

There is imprecision in floating point numbers, so avoid direct comparison. Also ‘someThreshold’ may be to be larger than you expect, especially for slower frame rates.

if ( object1.transform.position.z == object2.transform.position.z ) {
shootBullet code goes here;
}