Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Random chance out of 2

Discussion in 'Scripting' started by TheDiddyHop, May 2, 2014.

  1. TheDiddyHop

    TheDiddyHop

    Joined:
    Jul 26, 2013
    Posts:
    30
    So in my game i would like there to be a random chance that zombies get decapitated. i have read a few posts on the topic but cannot get it working.
    Code (csharp):
    1. var particles_blood : GameObject;
    2. var zombie_corpse : GameObject ;
    3. var decap_corpse : GameObject ;
    4. var slicing_sounds : GameObject ;
    5. var randValue = Random.value ;
    6.  
    7. function start () {
    8. randValue(0,1) ;
    9. }
    10. function OnCollisionEnter(collision : Collision)
    11. {
    12.   if ( collision.gameObject.tag == "Choppa" )
    13.   {
    14.     if (randValue < .45f);
    15.     {
    16.     Instantiate(particles_blood, transform.position, transform.rotation);
    17.     Instantiate(slicing_sounds, transform.position, transform.rotation);
    18.     Destroy (gameObject) ;
    19.     }
    20.     if (randValue <.10f);
    21.     {
    22.     Instantiate(slicing_sounds, transform.position, transform.rotation);
    23.     Instantiate(decap_corpse, transform.position, transform.rotation);
    24.     }
    25. }
    26. }
    Sorry if i've made a stupid mistake, but i have a limited knowledge of coding. Help would be much apprieciated as i've been going at this for the last couple of hours.
     
  2. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    I don't use javascript, but I am pretty sure this is the same as C#.
    You are actually trying to randomize a value between 0 AND 0.
    Random excludes always the last number and Random doesn't create float (unless I am mistaken).

    So you always get the same value which should be 0.

    Try to use this before your if(s) :
    Code (csharp):
    1.  Debug.Log("Here is the value of the random : " + randValue );
    You will always see what is the value of your randValue (to confirm).

    Hope this helps.

    PS: If you want to get some random value try to use a 100% where you randomize between 0 and 101. So you can keep your ratio easily.
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    Remove randValue(0,1) ; from your start function and instead of declaring it as Random.value declare it as a float and set it to a random value in the start function.

    Code (csharp):
    1. var randValue : float;
    2.  
    3. function Start () {
    4.  
    5.     randValue = Random.value;
    6.  
    7. }
    8.  
    9. function Update () {
    10.  
    11.     Debug.Log (randValue);
    12.  
    13. }
     
  4. TheDiddyHop

    TheDiddyHop

    Joined:
    Jul 26, 2013
    Posts:
    30
    Thank you, this worked perfectly :).
     
  5. TheDiddyHop

    TheDiddyHop

    Joined:
    Jul 26, 2013
    Posts:
    30
    Actually, i hadn't noticed it before but it clones the ragdolls. i have altered the script to allow a third ragdoll version.



    In the video it better illustrates my problem. I'm not sure how to fix this but i may go for a different method of gibbing so it is more precise.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Debug.Log your random value and post the results you get. Also, you might collide several times with the same collider as you don't disable/destroy them in the second if statement.
     
    Last edited: May 4, 2014