Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by sunrisepulp · Dec 19, 2017 at 03:54 PM · mathfminimum

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));


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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>()
 ) ;


Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sunrisepulp · Dec 19, 2017 at 04:34 PM 0
Share

@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!

avatar image Hellium sunrisepulp · Dec 19, 2017 at 04:54 PM 1
Share

I've added some comments to the 2 scripts. Tell me if something is not clear enough

avatar image sunrisepulp Hellium · Dec 19, 2017 at 05:15 PM 0
Share

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;
         }


Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges