2

I've written a script that makes the enemy appear behind the player, with a 1 in 5 chance every 3 minutes, I've changed the 1 in 5 chance to a 1 in 1 chance for testing purposes, but I'am unsure if it works.

I know how to place the enemy behind the player but not with a chance of 1 on 5.

I've tried this with a random number between 1 and 5. And that if the random number equals 1 he has to spawn the enemy and else not.

This is the code:

using UnityEngine;
using System.Collections;
public class MainEnemy : MonoBehaviour
{
    public GameObject Main;
    public GameObject Holder;
    public Transform player;
    public Transform FPSController;
    bool wantPosition;
    bool appear;
    float timer;
    Vector3 temp;
    // Use this for initialization

    void Start()
    {
        wantPosition = true;
        appear = false;
        temp = new Vector3(0, 0, 0);
        timer = 20;// 180;
    }

    // Update is called once per frame
    void Update()
    {
        Appear();
    }

    Vector3 GetPosition()
    {
        Debug.Log("Hij doet het getposition");
        float px = player.transform.position.x;
        float pz = player.transform.position.z;
        //
        float angle2 = Mathf.Cos(Camera.main.transform.eulerAngles.y) + 180;
        //
        float distance = 20;
        float distancex = Mathf.Cos(angle2 + 180) * distance;
        float distancez = Mathf.Sin(angle2 + 180) * distance;
        // Tell distanceen bij coordinaten op
        temp = new Vector3(distancex, -3, distancez);
        return temp;
    }

    void SetFalse()
    {
        if (timer < 1)
        {
            timer = 10;// 180; // na 3 minuten weer kijken of hij hem laat zien
        }
        Main.SetActive(false);
    }

    void Position()
    {
        if (wantPosition)
        {
            temp = GetPosition();
            Holder.transform.position = player.position + temp;
            Main.SetActive(true);
            if (timer < 0)
            {
                timer = 10; // kort zichtbaar
            }
            wantPosition = false;
        }
    }

    bool Appeared()
    {
        bool appear = false;
        int rand = Random.Range(1, 1);
        if (rand == 1)
        {
            appear = true;
        }
        else
        {
            appear = false;
        }
        return appear;
    }

    void Appear()
    {
        bool appear = false;
        if (timer <= 0)
        {
            appear = Appeared();
        }
        else
        {
            timer -= Time.deltaTime;
            if (timer < 10)
            {
                Debug.Log(timer);
            }
        }
        if (appear == true)
        {
            Position();
        }
        else
        {
            SetFalse();
        }
    }
}
user3071284
  • 6,955
  • 6
  • 43
  • 57
MaxSchulz
  • 21
  • 3
  • What exactly are you unsure about? Random int generation? Would be easiest to just make a little console application where you generate some random ints just to see if it works as you expect. – Marleen Schilt Dec 09 '15 at 10:16

3 Answers3

5

Firstly be aware that Random.Range returns a number that is greater than or equal to min, but less than (but not equal to) max. If the special case where min == max, then that value will be returned.

Secondly your Appeared function is a bit verbose. Assuming we set it back to a 1 in 5 chance, you'd have this:

bool Appeared()
{
    bool appear = false;
    int rand = Random.Range(1, 6);
    if (rand == 1)
    {
        appear = true;
    }
    else
    {
        appear = false;
    }
    return appear;
}

Firstly let me point out that saying:

if(x)
    y = true
else
    y = false

Where x is a Boolean condition, is exactly the same as saying

y = x

So perhaps you'd change it to:

bool Appeared()
{
    bool appear = false;
    int rand = Random.Range(1, 6);
    appear = rand == 1;
    return appear;
}

Now look, why bother setting appear to false at the start? That value never gets used. Perhaps this:

bool Appeared()
{
    int rand = Random.Range(1, 6);
    bool appear = rand == 1;
    return appear;
}

Hmm, now we're just assigning a variable and then returning it on the next line. OK, maybe this:

bool Appeared()
{
    int rand = Random.Range(1, 6);
    return rand == 1;
}

Right, yeah. This looks familiar. Now, again, we're almost just assigning a variable and then returning it on the next line. Do we really need that rand variable now? Probably not. How about this then:

bool Appeared()
{
    return Random.Range(1, 6) == 1;
}

Much better.

Richard Irons
  • 1,433
  • 8
  • 16
0

**EDIT Updated for UnityEngine

Change your bool Appeared method to:

bool Appeared()
{
   bool appear = false;
   int rand = Random.Range(1,6)
   if (rand == 1)
   {
      appear = true;
   } // else not needed
   return appear;
}
Steve Ford
  • 7,433
  • 19
  • 40
0

An easy and performant way to get a 1/5 chance in Unity3D is

if(Random.value < 1f/5f)
    //do something

Random.value is returning a float 0 <= value <= 1

AntiHeadshot
  • 1,130
  • 9
  • 24