- Home /
How to make a variable equal to Random.Range Result
Hello, I have been trying to solve an issue, probably so simple that it occurs because of my lack of JavaScript knowledge which I am trying to improve.
My aim is to create a function for attacking, including different weapons and damages by Random.Range values. Since I am planning to make this system dynamic, I want to pull data from current players weapon I defined above into "var currentWeapon", so random damage range will be displayed in it. To try this, I used temporary variables but I couldn't compile it.
function attack() {
daggerRoll = Random.Range(1,5);
clubRoll = Random.Range(1,7);
shortswordRoll = Random.Range(1,7);
longswordRoll = Random.Range(1,9);
greatswordRoll = Random.Range(1,13);
heavymaceRoll = Random.Range(1,9);
handaxeRoll = Random.Range(1,7);
greataxeRoll = Random.Range(1,13);
slingRoll = Random.Range(1,7);
shortbowRoll = Random.Range(1,7);
longbowRoll = Random.Range(1,9);
handcrossbowRoll = Random.Range(1,7);
heavycrossbowRoll = Random.Range(1,9);
var charDEXMod : float = 3; //TEMP
var charSTRMod : float = 5; //TEMP
var charBonus : float = 0; //TEMP
var currentWeapon = daggerRoll; //TEMP
if (charDEXMod > charSTRMod) {
charStatMod = charDEXMod;
}
else {
charStatMod = charSTRMod;
}
attackDamage = currentWeaponRoll + charStatMod + charBonus;
Debug.Log("Attack Damage: " + currentWeaponRoll +"+"+ charStatMod +"+"+ charBonus);
}
I also tried using it in a more static way like:
var dagger : float;
var club : float;
if (currentWeapon == dagger) {
currentWeaponRoll = Random.Range(1,5);
}
else if (currentWeapon == club) {
currentWeaponRoll = Random.Range(1,7);
}
but it only worked for the "if" statement above....
How can I solve this?
Thanks so much for your time!
FYI: charDEXMod > charSTRMod is to find if player/enemy is ranged or melee
How is it not working...what is the behavior? Note the integer version of Random.Range() is exclusive of the end value. For example Random.Range(1,5) produces the values 1,2,3,4 but not 5.
Thanks for your comment, RobertBU. I had been searching for the specifics on how C# converted float point values to integers for quite some time.
Answer by excalith · Dec 23, 2013 at 08:01 PM
It is a shame that I didn't notice I wrote currentWeaponRoll instead of currentWeapon just above debug.log... It is working now flawless... Since my MonoDevelop is broken, I am using another compiler to write codes, I missed the error I guess:)
Thanks so much for your time!
Your answer
Follow this Question
Related Questions
var TheCollision : Collision 1 Answer
How to make a variable double value? 1 Answer
Import Variables from another active script 1 Answer
Random.Range HELP!!! 2 Answers
One of my scripts can't find the variable from this script 1 Answer