-1

I'm currently making my first game using using Unity3D written in C#. What I'm doing now is the part where the player can get different attack by chance. eg. if it's critical chance is set to 20%, then it'll make a critical damage.

My problem is it's random number generator is making the same output for eg. when it makes a critical damage, it also make a stun damage when the requirements are met. What I want is, there's a different random value for critical, stun, etc. I have read that I can use Random.Next, but It's not working in Unity3D even though it's both C#. This is what I've done

 private float _criticalChance;
 private float _stunChance;

        // Use this for initialization
        void Start() {
            _criticalChance = Random.Range (0f, 1f);
            _stunChance = Random.Range (0f, 1f);
        }

Then I see in it's Debug.Log that it outputs the same value.

Aldrin Ramirez
  • 129
  • 1
  • 2
  • 12
  • Quick Question..have you looked at the [Unity Documentation](http://docs.unity3d.com/ScriptReference/Random.Range.html)..? – MethodMan Nov 08 '14 at 19:46
  • I've read that and that is where I've known the Random.Range to make random numbers. – Aldrin Ramirez Nov 08 '14 at 19:49
  • @DJKRAZE Link to documentation is indeed useful, but it explains nothing... It would be very surprising if whoever created the function did not read http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number/768001#768001 (also possible). – Alexei Levenkov Nov 08 '14 at 20:03
  • @AlexeiLevenkov I've already see that. And I saw that it also have a Random.Next which is not working in unity but you said is it possible? – Aldrin Ramirez Nov 08 '14 at 20:21
  • To clarify you claim: `Random.Range (0f, 1f) == Random.Range (0f, 1f)` is true all the time and `var generator= System.Random(); var r = generator.Next();` does not compile in Unity3d. Consider adding it to your post if it what you mean OR actually showing code that fails. – Alexei Levenkov Nov 08 '14 at 20:41

3 Answers3

0

you are making a random value only in start so it will stay this value till the end you have to put it in a method and call it when you want a new value

Milad Qasemi
  • 3,011
  • 3
  • 13
  • 17
  • According to what I've research, it is needed to put it in the start for initialisation. I have a method in stun, crit, etc. and I notice that they are all getting the same random value. – Aldrin Ramirez Nov 08 '14 at 20:23
  • you mean when you run _criticalChance = _stunChance – Milad Qasemi Nov 08 '14 at 20:25
  • I mean when my player has 0.2f critical && 0.1f stun, if the generated random value is 0.05, it will make a critical and also stun the enemy because their random values are the same. – Aldrin Ramirez Nov 08 '14 at 20:30
  • 3
    so _criticalChance != _stunChance , so 0.05 is a random value created for which one , i dont follow explain more – Milad Qasemi Nov 08 '14 at 20:38
0

It seems you want to update the value at different points in the game. Therefore you need to make functions that you can call.

 public float criticalChanceGenerator()
 {
      return Random.Range (0f, 1f);
 } 

 public float stunChanceGenerator()
 {
     return Random.Range (0f, 1f);
 } 

Then when an attack happens just call those functions.

public void attack()
{
    float stunChance = stunChanceGenerator();
    float criticalChance = criticalChanceGenerator();
}
apxcode
  • 7,696
  • 7
  • 30
  • 41
0

This is what I've do and it perfectly do want I want. I'm getting and error when I try to initialise a variable with a method so this is what I've do.

private float chance;
private int choice;

void Start() {
    chance = Random.Range(0f, 1f);
    choice = Random.Range(0, 2);
}

void Update() {
    chance = Random.Range(0f, 1f);
    choice = Random.Range(0, 2);
}

public void getHit(int damage) {
    if(chance <= fighter.attribute.criticalChance && chance <= fighter.attribute.stunChance) {
        if(choice == 0) {
            attribute.maxHealth -= damage + (int)(damage * fighter.attribute.criticalDamage);
        } else {
            getStun(fighter.attribute.stunTime);
            attribute.maxHealth -= (int)damage;
            fighter.attribute.resetStun();
            stunned = true;
        }
    }
}
Aldrin Ramirez
  • 129
  • 1
  • 2
  • 12