I am making a game where you play in a battle mech and it’s supposed to show the elevation between you and the enemy, I tried making some scripts but none worked.
Any help would be nice,
I am making a game where you play in a battle mech and it’s supposed to show the elevation between you and the enemy, I tried making some scripts but none worked.
Any help would be nice,
What you need to do. Is have a list with all the enemies that are currently existing. This list should be held by a game manager or something like that. Then every frame you just iterate over the list to compare every enemy’s y position with the players’ y and keep the one that results in the smallest value. Then just display that.
I can give you some pseudo code.
//In some kind of manager object
List<Enemy> enemyList;
Player player;
Inside Update
//Set smallestDifference to the difference from the first enemy in the list
float smallestDifference = Mathf.Abs(player.pos.y - firstEnemyInList.pos.y);
Iterate over enemyList skipping the first enemy in the list
float difference = Mathf.Abs(player.pos.y - enemy.pos.y)
if difference < smallestDifference
smallestDifference = difference
Display smallestDifference in the UI
Of course you would first need to populate the enemyList with every enemy and if more enemies spawn during the game you’d need to keep the list updated.
This should point you in the right direction. If you have questions don’t hesitate to ask!
Edit: Another way to do this would be to have the list sorted by enemy y position on ascending order, every frame. Then just get the first enemy on the list and get the difference from that.
Note: You should post the code you’ve tried using to solve this so we can help you with it. Just making scripts for other people is not something encouraged by the guidelines of this forum
Thanks, this helped alot!