Mathf.Min, get which object is the minimum?
Hello there,
I have this code here where I use a Mathhf.min, and it works out what the closest object is. But it only spits out the distance of what is closest, i want to know what object that is, can I put something in here to get back which of these object is the minimum and then so i can use it in a if statement?
Debug.Log(Mathf.Min(player1.GetComponent<PlayerMovement>().distToPuck , player2.GetComponent<PlayerMovement>().distToPuck, player3.GetComponent<PlayerMovement>().distToPuck));
Answer by Hellium · Dec 19, 2017 at 04:11 PM
Try this :
// Create a new list containing the PlayerMovement scripts
System.Collections.Generic.List<PlayerMovement> list = new System.Collections.Generic.List<PlayerMovement>()
{
player1.GetComponent<PlayerMovement>(),
player2.GetComponent<PlayerMovement>(),
player3.GetComponent<PlayerMovement>()
};
// Sort the list according to the `distToPuck`, in the ascending order.
// The Sort method can take as argument a function used to compare the elements in the list
// I've created an anonymous function to compare the distToPuck
list.Sort( ( PlayerMovement p1, PlayerMovement p2 ) =>
{
if ( Mathf.Approximately( p1.distToPuck, p2.distToPuck ) )
return 0;
else
return (int) Mathf.Sign( p1.distToPuck - p2.distToPuck );
} ) ;
// The list is now sorted. The first element of the list is the one with the smallest distToPuck value
PlayerMovement bestPlayerMovement = list[0];
2nd solution :
public PlayerMovement GetBestPlayerMovement( params PlayerMovement[] playerMovements )
{
// Consider the "best" as the 1st one of the array
PlayerMovement best = playerMovements[0];
// Save the distToPuck as the "best" one (the smallest one)
float minValue = best.distToPuck ;
// Loop through the array in order to find the playerMovement with the smallest distToPuckValue
for ( int i = 1 ; i < playerMovements.Length ; i++ )
{
if ( playerMovements[i].distToPuck < minValue )
{
best = playerMovements[i];
minValue = playerMovements[i].distToPuck;
}
}
return best ;
}
And call it :
PlayerMovement playerMovement = GetBestPlayerMovement(
player1.GetComponent<PlayerMovement>(),
player2.GetComponent<PlayerMovement>(),
player3.GetComponent<PlayerMovement>()
) ;
@Hellium Oh that looks like its above my pay grade, can you explain further what it means? Cant seem figure out how to use it exactly.
But thanks for answer though!
I've added some comments to the 2 scripts. Tell me if something is not clear enough
Hmm.. made me understand it a bit better, but its not the part of i have trouble with sadly. I want to activate the $$anonymous$$mmate as you wrote the code, where it finds the closest to the puck, if the code finds the one that is closest, where in the code does it "Spit it out" so to speak?
I though about activating it like this with simple if statements:
//Activate control of the players.
if (ChoosenPlayer1 == 1)
{
//Switch to Teammate 1.
player1.GetComponent<Player$$anonymous$$ovement>().activate = true;
//Disables the control of the other players.
player2.GetComponent<Player$$anonymous$$ovement>().activate = false;
player3.GetComponent<Player$$anonymous$$ovement>().activate = false;
}
else if(ChoosenPlayer1 == 2)
{
//Switch to Teammate 2.
player2.GetComponent<Player$$anonymous$$ovement>().activate = true;
//Disables the control of the other players.
player1.GetComponent<Player$$anonymous$$ovement>().activate = false;
player3.GetComponent<Player$$anonymous$$ovement>().activate = false;
}
else if(ChoosenPlayer1 == 3)
{
//Switch to Teammate 3.
player3.GetComponent<Player$$anonymous$$ovement>().activate = true;
//Disables the control of the other players.
player1.GetComponent<Player$$anonymous$$ovement>().activate = false;
player2.GetComponent<Player$$anonymous$$ovement>().activate = false;
}
Your answer
Follow this Question
Related Questions
How to get all array float values then do Mathf.Min? 1 Answer
Unit Circle Coordinates 0 Answers
Rotation Direction 2 Answers
Problems with instantiated objects in a circle formation 0 Answers
Mathf.Clamp does not work at all. 1 Answer