Hey, I am trying to make an Endless Runner where the player is being chased by something. I am currently working on making the ground instantiate as the player runs through OnTriggerEnter() game objects to trigger the instantiation. I am having issues with this as the ground instantiates the first time into the new placement, but the next time it simply overlaps the first instantiation. I have pictures to help explain this…
Screenshot by Lightshot < – This is on starting the game. There is the Edging on trigger GO and the spawner location.
Screenshot by Lightshot ← This is upon hitting the first Edging GO, the new ground tiling, edging GO and spawner GO are instantiated.
Screenshot by Lightshot < – This is upon hitting the second set of instantiated GOs, they overlap into the same place.
#pragma strict
var onLeft : boolean = true;
var onRight : boolean = true;
var speed : float;
var GameO : Canvas;
//Lava Linecast
var lava : Transform;
var body : Transform;
var consumed : boolean = false;
//Instantiation
var edging : Transform;
var spawner : Transform;
var spawned : boolean = false;
var ground : GameObject;
function Start () {
GameO = GameO.GetComponent(Canvas);
GameO.enabled = false;
}
function Update () {
Lava();
if(onLeft) {
if(Input.GetKeyDown("f")) {
transform.position.x += speed;
onLeft = false;
onRight = true;
}
} else if(onRight) {
if(Input.GetKeyDown("j")) {
transform.position.x += speed;
onRight = false;
onLeft = true;
}
}
}
function OnTriggerEnter2D (other : Collider2D) {
Debug.Log("HI");
if(other.CompareTag("Spawn")){
Spawn();
}
}
function Spawn() {
Debug.DrawLine(edging.position, spawner.position, Color.green); //Edge Line
spawned = Physics2D.Linecast (edging.position, spawner.position, 1 <<LayerMask.NameToLayer("Edge"));
if (spawned) {
var clone : GameObject;
clone = Instantiate(ground,spawner.position,spawner.rotation);
}
}
function Lava() {
Debug.DrawLine(body.position, lava.position, Color.green); //Lava Line
consumed = Physics2D.Linecast (body.position, lava.position, 1 << LayerMask.NameToLayer("Lava"));
if (consumed) {
GameO.enabled = true;
}
}
Help would be lovely on this as it feels like it is frying my brain, also if there is any other information I can provide please let me know and I will be sure to do my best.