Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Endless Runner Instantiate Issue...

Discussion in 'Scripting' started by Pyronide, Jun 17, 2015.

  1. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    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...

    http://prntscr.com/7i1bu6 < -- This is on starting the game. There is the Edging on trigger GO and the spawner location.

    http://prntscr.com/7i1cdv <-- This is upon hitting the first Edging GO, the new ground tiling, edging GO and spawner GO are instantiated.

    http://prntscr.com/7i1cvm < -- This is upon hitting the second set of instantiated GOs, they overlap into the same place.

    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4. var onLeft : boolean = true;
    5. var onRight : boolean = true;
    6. var speed : float;
    7.  
    8. var GameO : Canvas;
    9.             //Lava Linecast
    10. var lava : Transform;
    11. var body : Transform;
    12. var consumed : boolean = false;
    13.             //Instantiation
    14. var edging : Transform;
    15. var spawner : Transform;
    16. var spawned : boolean = false;
    17.  
    18. var ground : GameObject;
    19.  
    20. function Start () {
    21.     GameO = GameO.GetComponent(Canvas);
    22.     GameO.enabled = false;
    23. }
    24.  
    25. function Update () {
    26.     Lava();
    27.    
    28.     if(onLeft) {
    29.         if(Input.GetKeyDown("f")) {
    30.             transform.position.x += speed;
    31.             onLeft = false;
    32.             onRight = true;
    33.         }
    34.     } else if(onRight) {
    35.         if(Input.GetKeyDown("j")) {
    36.             transform.position.x += speed;
    37.             onRight = false;
    38.             onLeft = true;
    39.         }
    40.     }
    41. }
    42.  
    43. function OnTriggerEnter2D (other : Collider2D) {
    44.     Debug.Log("HI");
    45.     if(other.CompareTag("Spawn")){
    46.         Spawn();
    47.     }
    48. }
    49.  
    50. function Spawn() {
    51.     Debug.DrawLine(edging.position, spawner.position, Color.green); //Edge Line
    52.     spawned = Physics2D.Linecast (edging.position, spawner.position, 1 <<LayerMask.NameToLayer("Edge"));
    53.     if (spawned) {
    54.         var clone : GameObject;
    55.         clone = Instantiate(ground,spawner.position,spawner.rotation);
    56.     }
    57. }
    58.  
    59. function Lava() {
    60.     Debug.DrawLine(body.position, lava.position, Color.green); //Lava Line
    61.     consumed = Physics2D.Linecast (body.position, lava.position, 1 << LayerMask.NameToLayer("Lava"));
    62.     if (consumed) {
    63.         GameO.enabled = true;
    64.     }
    65. }

    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.
     
    Last edited: Jun 17, 2015
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    Updated with code in the post, I was having some issues when copy pasting, also the pictures, when put inside the post, they came up with broken links, so I don't know what happened... sorry for this
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you check whether the platform ends up at the spawner's position with the correct rotation?
     
  5. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    Yes, it is not rotated, it is being put in the same place x20.94 y-2.18 it should be x30 something or just shy
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It looks like the wrong spawn transform is used to place the ground.
     
  7. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    I fixed this by putting the spawner in an empty game object and then manipulating the object every time OnTiggerEnter() was called

    Thanks for your help!