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.
var particles_blood : GameObject;
var zombie_corpse : GameObject ;
var decap_corpse : GameObject ;
var slicing_sounds : GameObject ;
var randValue = Random.value ;
function start () {
randValue(0,1) ;
}
function OnCollisionEnter(collision : Collision)
{
if ( collision.gameObject.tag == "Choppa" )
{
if (randValue < .45f);
{
Instantiate(particles_blood, transform.position, transform.rotation);
Instantiate(slicing_sounds, transform.position, transform.rotation);
Destroy (gameObject) ;
}
if (randValue <.10f);
{
Instantiate(slicing_sounds, transform.position, transform.rotation);
Instantiate(decap_corpse, transform.position, transform.rotation);
}
}
}
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.
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) : 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.
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.
var randValue : float;
function Start () {
randValue = Random.value;
}
function Update () {
Debug.Log (randValue);
}
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.