Finding Positions of other game objects and using them as an argument for an if statement.

Hey guys, First of all ill just say thank you to everyone who has helped me on this forum, your all a bunch of legends.

Ok so im new to programming and i have attempted to write a code that finds the y position of an enemy and then uses that y position as an argument for a “if statement”.
Im creating a sidescroll platformer game like mario and what im trying to accomplish with this code is when my character jumps on an enemies head, they die, but if the player touches the enemy from side, then the player looses health.
How i think this could be done is by finding the y position of the enemy and then creating an if statement that says (if player is higher then enemy) blah blah blah.

The code i have got so far is this…

target = GameObject.FindWithTag("enemy").transform.y;
myPosition = transform.position.y;

if(myPosition > target){
Debug.Log("player is up higher then enemy");

can anyone help?
i also just noticed that if i use (findwithTag) then the code might find all the enemies in the level and activate the code when its not supposed too, is that right?. an example would be if enemyA is sitting on a hill and i run into the side of enemyB then the code would activate because they are both tagged with “enemy”

Help, please. any advice will be greatly appreciated.


Hey Davidc, I can tell you are on the right track. merry_christmas has a good idea. If you put the collision logic on a script that the enemy has you only have to worry about the players position. The player position can be a static variable that collision logic script will hold. This ensures the player position variable is the same for all instances of the collision logic script. One problem you have is that you don’t take into account the player/enemy’s x position. This will ensure that only the right enemy will be hit. So you have to check if the player is above the enemy(which will be this.transform.position.y - if the script is attached to the enemy) and you have to check that the player is within the bounds of the x value. To do this you’ll need to know where the anchor/transform of the enemy is. If its in the center the x bounds will be:

    		(this.transform.position.x - enemyWidth / 2) //this is the left x bounds
    		(this.transform.position.x + enemyWidth / 2) //this is the right x bounds

The enemyWidth variable is the lenght of the sprite/texture of the enemy on the x axis. Becuase you are in the cetner half the width of the enemy’s texture/sprite gets you to the far left or right of the enemy.
If the transform/anchor is on the far left of the enemy the code would be:

		(this.transform.position.x) //this is the left x bounds
		(this.transform.position.x + enemyWidth) //this is the right x bounds

So by check the x value of the player against the x bounds of the enemy you will know if the player is actually on top of the enemy or just jump somewhere else. I hope this help, from what I can tell your a smart man on the right path just think and you’ll get through this. :smiley: