Search Unity

Help With making something random but not totally random.

Discussion in 'Scripting' started by Jorjor210, May 5, 2015.

  1. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I am currently trying to make a script that allows my 4 turrets to shoot at different times. but I want the shooting order to change after every loop. I need a script that makes the shooting order random. but never have 2 turrets shooting at the same time. example:

    if turret1 is shooting after 1 second. turret2 cant shoot after 1 second so it shoots and 3 seconds. turret3 cant shoot at 1 or 3 seconds so it shoots after 2 seconds....Next loop, turret1 shoots at 2 seconds. turret2 cant shoot at 2 seconds so it shoots at 1 second. turret3 cant shoot at 1 or 2 so it shoots at 3 seconds.

    how would I make a script that does this action? my Turrets current script is:
    Code (JavaScript):
    1. var LookAtTarget: Transform;
    2. var damp=6.0;
    3. var BulletPrefab: Transform;
    4. var savedTime=0;
    5. var delayTime=2f;
    6. var StartTime=3;
    7. function Update ()
    8. {
    9.     if(LookAtTarget)
    10.     {
    11.         var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
    12.      
    13.         var seconds: int = Time.time;
    14.      
    15.         Shoot(seconds);
    16.     }
    17. }
    18.     function Shoot(seconds)
    19.     {
    20.         if (seconds == savedTime)
    21.         {
    22.             var bullet = Instantiate(BulletPrefab ,transform.Find ("SpawnPoint").transform.position ,
    23.                                     Quaternion.identity);
    24.             bullet.GetComponent.<Rigidbody>().AddForce(transform.up *1000);
    25.          
    26.             savedTime=seconds+delayTime;
    27.             }
    28.             }
    29.      
    30.            
    I'm aware it isn't super neat. but I'm new to coding and it at least shoots.
    Having that, I tried multiple different methods of making a script that allows to do the action I want to. But I just cant make it happen. Can anyone please help? I've been working on this small problem for 3 days. (In javascript please)
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Suppose this were a card game. You have one card for each turret. The algorithm would be simple: shuffle the cards, draw 1 card each second (and shoot that turret), and repeat. When the deck is empty, reshuffle the discards.

    Now, sadly, C# doesn't have a shuffle method built in. I've got one in my standard bag of tricks, but even more sadly, you're using JS. ;)

    However, you don't really need to shuffle — you need to draw one at random, which is easy to do. I don't speak JS anymore, so I'll just give it to you in pseudo-code, and you can write it up in JS. This all goes in the Update method:
    • if it's not time to shoot yet, return.
    • check our list of turrets that haven't fired yet; if it's empty, initialize it with all our turrets.
    • pick one at random. Fire that turret, and remove it from the list.
    • Set the next-shoot-time to the current time plus one second (or whatever interval you like).
    That's it. Implement each of those four steps carefully, and you'll have it! But if you have any trouble, post back here and get some help. And if you ever want to reexamine your life choices and make the switch to C#, never fear, we'll help with that too. :)
     
  3. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I'd love to switch over to C# but from my knowledge (which at the currently as a vast as a subatomic particle) you can't code a C# script to "communicate" with JavaScript. And although I do understand C# (but very little) I could not for the life of me translate my javascript to C# so that this would work. I'm open for all suggestions of course :) if you have any ideas please share :) and thank you!
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
  5. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
  6. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Maybe so, but it's not very polite. There are enough new posts in these forums already, and if anyone else finds the thread later it's much better to have all the information in a single thread.
     
  7. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I apologize for offending you, and i appreciate your help a lot. I honestly just needed a second opinion because I cant get it working. it's nothing against you I just figured if I made anew post someone would be able to help resolve our current issues. and i feel rude continually bothering you with errors. So i just wanted to get a second opinion.
     
  8. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    No worries, its not a big deal and I'm not at all personally offended :)
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    So, how's it going? I gave you the solution above; have you tried to implement it? On which step (if any) did you get stuck?