How to allow the player to change the mass/force of an object in the UI? Noob question

I am working on a project where a penguin is launched at a certain force with a certain mass (see the UI in the right).
Noob question, but how can I make it so that the player can input a force and mass of their own choosing within the UI? (i.e. an input or drop down list, that will affect a variable?)

Thank you!

Independent from your implementation of the UI, you would create a field corresponding to your force and mass. I would say you should probably do a float field. That gives the user the maxiumum flexibility.

Depending on how you implemented the GUI one possible approach would be GUI.TextField, which returns the string entered, so you can retrieve its value in code. To make it work as a float field you will have to write some validation and parsing code, here’s a UA-thread that can give you a direction.

If you are using unitys canvas system, the Unity UI InputField could be of use to you. You would need to validate it too.

in the most simple way, to use the value of the input field in code, you will want to create a reference to the input field return value and then use that in your game logic (coupled), or if you want to go the proper route, you create an event that parses the input value and fires everytime the value in the input field changes. You would then subscribe to that event in your game logic, changing the value of your mass and velocity according to the event parameters.

I would suggest making a inputfield, detecting the int value of what is written, storing it in a public int value and then applying that force or mass to it.


There are lot’s of tutorials on how to use the inputfield, I recommend checking out one of them that works for you.