- Home /
Instatiate inside OnTriggerEnter gives unwanted delay
Hello Everyone! I'm trying to create a scrolling street. Each time the street hits a box collider, more of the street (a new "street segment" if you will) is generated off screen so that it appears endless. My problem is that when I generate the next street segment there is a variable delay, creating openings between segments. This is especially apparent when I boost the speed at which the segments scroll. Here is my code currently; it is attached to an empty game object with a box collider called "StreetCollider":
#pragma strict
//Detects when a street hits the collider, creates and deletes streets
var nextStreet : GameObject; //Pass it the next street to create
//Next step is to create randomstreet generator script and pass return
//value to this variable. Make this variable private.
function OnTriggerEnter (currentStreet : Collider) {
//Get some data from the current street
//This helps to get the size of the currentStreet object's mesh
var mf : MeshFilter = currentStreet.GetComponent(MeshFilter) as MeshFilter;
var mesh = mf.sharedMesh;
//Gets the mesh size and scale
var size = mesh.bounds.size;
var scale = currentStreet.transform.localScale;
//Create a clone of what's passed to nextStreet
if(Instantiate(nextStreet, Vector3(0, 0, -size.y*scale.y), currentStreet.transform.rotation))
Debug.Log("Street collider collided with!");
}
function OnTriggerExit (currentStreet : Collider) {
//Destroy the currentStreet when it exits the trigger
Destroy(currentStreet.gameObject);
}
I'm a programmer but I'm relatively new to the concepts of scripting versus more programmatic coding, so I'm a bit hazy on encapsulation methods and message passing in Javascript. Would calling a different function outside of OnTriggerEnter help? Would creating some of the data outside of OnTriggerEnter help and if so, where would I put the data? Could a possible fix include the FixedUpdate function?
Thank you for reading this and any help is greatly appreciated!
It's kind of unclear what you mean by delay. Are you referring to time delay or position offset?
Sorry, I meant position offset. I've scrapped the idea altogether to scroll the street and ins$$anonymous$$d opted to have the player progress the street's generation. I'm hoping this will cut out the lag.
Let me know if it won't ;-) And if it does work out, please post an answer to your question and mark it as accepted.
Answer by kaiserkappel · Dec 17, 2012 at 03:21 PM
I've since decided to move the character and other NPCs on top of the static generated street segments. This is working a lot better so far.
Your answer
Follow this Question
Related Questions
How to Select between two prefabs randomly ? 1 Answer
Simple Instantiate gameObject problem 3 Answers
Instantiate object in for loop with array 2 Answers
Instantiate without (clone) in JS. 1 Answer