I am new to scripting and everything. And I am currently trying to make a game that consists of 4 turrets. I want each turret to have different shoot times at all times. But I don’t want each turret to have a set shooting time. I was thinking of trying to create a script with a random number picker between (1-5). And from there I wanted to each number to represent a fire time. Then from there I would need to somehow communicate between turret scripts tht if one is shooting in 1 second then none of the other can shoot at 1 second. And so on and so forth. Does this make sense? If so is it possible? (I would loop the random number picker so tht it would switch shooting times after every shot) suggestions and tips are encouraged!
It would probably be easier to have a script that manages all the turrets and chooses which should fire. Something like this (not tested, but should work):
public class FireManager : MonoBehaviour
{
public MyTurretScript[] myTurrets;
void Start()
{
StartCoroutine(FireTurrets());
}
IEnumerator FireTurrets()
{
// Select a turret
int r = Random.Range(0, myTurrets.Length);
myTurrets[r].Fire();
// Wait a random time
float t = Random.Range(1f, 5f);
yield return new WaitForSeconds(t);
}
}
This assumes you have a MyTurretScript on your turrets, and assign them in the inspector. This script should have a Fire method to handle the actual firing.
any chance you could retype this into JavaScript? i cant access my other js from C#
I don’t usually code in js, but something like this maybe:
var myTurrets : MyTurretScript[];
function Start()
{
StartCoroutine(FireTurrets());
}
function FireTurrets()
{
var r = Random.Range(0, myTurrets.Length);
myTurrets[r].Fire();
var t = Random.Range(1.0, 5.0);
yield WaitForSeconds(t);
}
key getting error: “array index is out of range”
IndexOutOfRangeException: Array index is out of range.
TurretTimer+$FireTurrets$6+$.MoveNext () (at Assets/TurretTimer.js:11)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
TurretTimer:Start() (at Assets/TurretTimer.js:5)
Any idea what might be happening?
If you have 4 turrets, to access the 4th turret, the index is 3, because the index starts with 0, not 1. So this would be:
var r =Random.Range(0, myTurrets.Length - 1)
Have you assigned the turrets in the inspector?
No, Random.Range "Returns a random integer number between min [inclusive] and max [exclusive] ", so with 4 turrets, Random.Range(0, myTurrets.Length) will return a number between 0 and 3.
Yes I have, Still get that error.
That’s strange, I don’t see why it shouldn’t work. Just to make sure, what do you get if you print the length of myTurrets to the console? Like so:
function FireTurrets()
{
Debug.Log(myTurrets.Length);
var r = Random.Range(0, myTurrets.Length);
myTurrets[r].Fire();
var t = Random.Range(1.0, 5.0);
yield WaitForSeconds(t);
}
Click to see full size.
Idk why it says 0 though.
Make an if statement to check if r is less than the turret array length. Should resolve it for good.
Yeah that’s strange. I see only see a different mistake in my script, there should be a loop inside the FireTurrets function.
I created a test project, see the attachment. Try this in an empty project and see if you can figure it out.
2100409–137527–turrets.unitypackage (5.41 KB)
Hmm… Idk why yours is working and mine isn’t. here’s the code to my turret if that solves anything.
var LookAtTarget: Transform;
var damp=6.0;
var BulletPrefab: Transform;
var savedTime=0;
var delayTime=2f;
var StartTime=3;
function Update ()
{
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
var seconds: int = Time.time;
Shoot(seconds);
}
}
function Shoot(seconds)
{
if (seconds == savedTime)
{
var bullet = Instantiate(BulletPrefab ,transform.Find ("SpawnPoint").transform.position ,
Quaternion.identity);
bullet.GetComponent.<Rigidbody>().AddForce(transform.up *1000);
savedTime=seconds+delayTime;
}
}
Btw. i noticed your script shoots one turret at a given interval, which is what i want. but i want it to assign a shooting time for each turret and then loop and switch.
a good example would be that joe strout made: there’s 4 turrets and 4 cards. the for cards have the times 1,2,3, and 4. you shuffle the deck and assign the cards to the turrets. turret1 gets 2 seconds, turret2 get 1 second, turret3 gets 4 seconds, turret 4 gets 3 seconds… after the actions is complete you reshuffle the cards and re-assign them.
Ok, I see what you mean, try it like this
var myTurrets : Turret[];
function Start()
{
StartCoroutine(FireTurrets());
}
function FireTurrets()
{
// Create a copy of the original array
var randomTurrets = new Turret[myTurrets.length];
System.Array.Copy(myTurrets, randomTurrets, myTurrets.Length);
while (true)
{
// Randomize the array
System.Array.Sort(randomTurrets, function(t1,t2) Random.Range(-1, 2) );
// Loop through array and fire
for (i = 0; i < randomTurrets.length; i++)
{
randomTurrets[i].Fire();
var t = Random.Range(1.0, 5.0);
yield WaitForSeconds(t);
}
}
}
If you post the full code of your turret timer script I can try to see what could be wrong with it.
2100526–137538–turrets2.unitypackage (5.57 KB)