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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

[SOLVED] Random Encounter - Random.Range with Delay

Discussion in 'Scripting' started by akadird, Nov 6, 2019.

  1. akadird

    akadird

    Joined:
    Dec 19, 2017
    Posts:
    11
    I'm a new GD who trying to make a turn based rpg.
    I'm try to make a random encounter system.
    Every area has a monobehaviour only keeps scriptableobject that saves areaInfo. Every area has an int difficulty and secToRoll variable. When player enters an area waits for 'secToWait' time then takes a Random number between 1-100 and checs if its below to difficulty of that area. If it is then start to CombatStat, if it's not then wait another 'secToWait' time and take another Random number.
    So i tried some diffrent things and i faild. Number 1 problem that i chouldn't manage to solve is:
    When player enters the area waits "SecToWait" time and takes a random number but after that its keep taking random numbers every frame until if(randomNumber<=difficulty) statement is true.

    I don't know i write right and understandable(Eng not my primary language...abviously!) But i really need some help here :D Thnks.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Hi and welcome!

    It would help if you showed your actual code, but it seems like you messed the conditions up.
    Algorithmically this should look as follows:
    Variables: secBetweenRolls = seconds between rolls, curTime = now / the tickcount, lastRollTime = time of last roll
    Behavior: You want to check every frame if curTime is at least as high as (secBetweenRolls + lastRollTime). If not, do nothing. If so, set lastRollTime = curTime, roll a random number and do your test for whether it's under the difficulty.

    That's basically it. Since after each check you set lastRollTime to the current time, you always wait secBetweenRolls seconds before the condition turns true again. If you start a battle, at the end of it you should set lastRollTime to now aswell tho, since otherwise you may directly roll for the next battle which may be annoying.
     
  3. akadird

    akadird

    Joined:
    Dec 19, 2017
    Posts:
    11
    Well that was really easy and i feel stupid a little :D Thank You Very Much.