When my player car hits a traffic trigger box, my script searches for an inactive car and sets it at the position of a spawner. Each traffic box has several spawners. Both the spawners and cars are stored in arrays.
For some reason, my code isn’t working. The cars don’t align properly with the spawners, both rotation and position. Some spawners are worse than others. It’s really strange… most of the time, the cars are put exactly where the spawners are but on a couple of them, the car is placed off to the side and it goes through the wall. Also, the rotation is never right.
What’s wrong with my code? I thought it looked pretty decent and should work but I’m having no luck…
function OnTriggerEnter(Other : Collider)
{
if (Other.tag == "Player")
{
if (Traffic_Script.g_iCarsActive < Traffic_Script.g_iMaxActiveCars)
{
iRandom = Random.Range(0, SpawnerArray.Length + 1);
for (var i = 0; i < iRandom; ++i)
{
iIndex = Random.Range(0, AICarArray.Length);
if (!AICarArray[iIndex].active)
{
AICarArray[iIndex].transform.position.x = SpawnerArray*.transform.position.x; // Set car pos to spawner pos*
_ AICarArray[iIndex].transform.position.y = SpawnerArray*.transform.position.y + 1;_
_ AICarArray[iIndex].transform.position.z = SpawnerArray.transform.position.z;
AICarArray[iIndex].transform.rotation = SpawnerArray.transform.rotation;
AICarArray[iIndex].SetActive(true);
}
else*
* {
print(“Car already active”);
}
}
}
}
}*_