So I’m learning C# via Unity for the first time and I figured I’d try and get a pretty simple math equations output to appear in an input field if another number was put in another input field, basically dnd’s Ability Scores and Modifiers. But I’m having a hard time finding a straight forward answer that isn’t 30 minutes long. I’ve added using System.Math to the code and that’s about it.
The equation is (User input - 10)/2=Modifier rounded down
So it’d be like: 10 = 0, 12 = 1, 14 = 2, 16 = 3, 18 = 4, 20 = 5, and so on
Does anybody know how that would go in C#? I know in javascript it’d be like:
var strScore = document.getElementById(“strScore”).value;
document.getElementById(“strMod”).value = Math.floor((strScore - 10)/2);
And I figure it might be similar but I can’t be sure.
Mathf.FloorToInt((score - 10f)/2f)
Assuming you’ve already parsed your user input into an int score
variable.
It’s a bit unclear from your question if you are having trouble with the math part or if you’re having trouble parsing a string to an int, or something else.
To be honest all of it. Like I said I just started, so parsing the int I forgot about being a thing but that is something you kind of have to do in Javascript. To get into more detail: there are 6 ability scores, in each the user inputs a number and the modifier is the output of the equation. Would simply parse.int work or would it have to be something like parse.int(“STR Score”);? STR Score being the input field the user fills. And then would you just input Mathf.FloorToInt((score - 10f)/2f) or would it be Mathf.FloorToInt((“STR Score” - 10f)/2f);? Would I also need to have the modifier named in this equation somehow?
Pictures for a visual reference. Top input field for the score, bottom for the modifier. Added the hierarchy for reference, ignore the skills, that’s for another part that’s dependant on this part getting done first lol.