I’m trying to make an object/objects instantiate when OnTriggerEnter is activated.
At first this worked fine, the objects appeared when the trigger was entered just as needed.
Original script-
using UnityEngine;
using System.Collections;
public class HazzardSpawner : MonoBehaviour
{
public GameObject hazzardType;
void OnTriggerEnter()
{
Instantiate (hazzardType, transform.position, transform.rotation);
Destroy(gameObject);
}
}
Now I get a little more technical and want to add a variation along the x axis to where they spawn.
new script-
using UnityEngine;
using System.Collections;
public class HazzardSpawner : MonoBehaviour
{
public GameObject hazzardType;
public int spawnVariation;
void OnTriggerEnter()
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnVariation, spawnVariation), 0, 0);
Instantiate (hazzardType, spawnPosition, transform.rotation);
}
}
It adds the variation along the x axis, but for some reason its also affecting where the objects spawn along the z axis. Every time its spawning the objects about 20 units away from the origin point on the Z axis.
???
This is the trigger(highlighted) and its origin point.
I’ve added this sphere to show where the objects are now spawning from(y axis)