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. Dismiss Notice

Instantiate GameObject on every touch position (fluidity issue)

Discussion in 'Scripting' started by Arkolve, Dec 28, 2014.

  1. Arkolve

    Arkolve

    Joined:
    Dec 29, 2013
    Posts:
    20
    Hi, I'm trying to implement a script that instantiate a GameObject with a sprite renderer (2D) and it's working but not exactly as I want : when the finger is moving slowly it's great but when it comes to move faster, it leaves spaces between the sprites as shown in the picture (left side is made with slow moves and right side is made by faster finger moves).

    My script :
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var test : GameObject;
    4. private var touchID : int = -1;
    5. function FixedUpdate () {
    6.     var count = Input.touchCount;
    7.  
    8.     for(var i : int = 0; i < count; i++)
    9.     {
    10.         var touch : Touch = Input.GetTouch(i);
    11.      
    12.         if ( touch.phase == TouchPhase.Began ) {
    13.                 touchID = touch.fingerId;
    14.             } else if (touchID == touch.fingerId) {
    15.                 var tempPos : Vector3 = touch.position;
    16.                 tempPos.z = 10;
    17.                 var instance : GameObject = Instantiate(test, camera.ScreenToWorldPoint(tempPos), Quaternion.identity);
    18.             }
    Any suggestions to make this script stronger please ?
     

    Attached Files:

  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Maybe keep a running list of the current object created, and the last object created. If distance between greater than x, Instatiate another between.
     
    Kiwasi likes this.
  3. Arkolve

    Arkolve

    Joined:
    Dec 29, 2013
    Posts:
    20
    Thank you for your answer but it doesn't work, I also tried to put a huge array of GameObject already instantiated and move them rather than create them but there are always spaces..
     
  4. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    @renman3000's suggestion was spot on. If it doesn't work then you're not implementing it correctly.

    That's a separate issue from then one you're having. Unity's input system will update the positions it reports to you every frame, not every X distance traveled. If fingers move faster then the distance between touche will be greater, and you'll have to fill in the gaps between the objects you're creating yourself.
     
    Last edited: Dec 29, 2014
  5. Arkolve

    Arkolve

    Joined:
    Dec 29, 2013
    Posts:
    20
    I tried something like that (a rough draft) :
    Code (JavaScript):
    1. #pragma strict
    2. var speed : int = 50;
    3. var snowTile : GameObject;
    4.  
    5. private var touchID : int = -1;
    6. private var oldInstance : GameObject = null;
    7. private var newInstance : GameObject = null;
    8.  
    9. function Start() {
    10.  
    11. }
    12. function FixedUpdate () {
    13.     var count = Input.touchCount;
    14.     var distanceNewtoOld : float;
    15.  
    16.     for(var i : int = 0; i < count; i++) {
    17.         var touch : Touch = Input.GetTouch(i);
    18.      
    19.         if ( touch.phase == TouchPhase.Began ) {
    20.                 touchID = touch.fingerId;
    21.              
    22.                 var tempPos : Vector3 = touch.position;
    23.                 tempPos.z = 10;
    24.              
    25.                 oldInstance = Instantiate(snowTile, camera.ScreenToWorldPoint(tempPos), Quaternion.identity);
    26.             } else if (touchID == touch.fingerId) {
    27.                 tempPos = touch.position;
    28.                 tempPos.z = 10;
    29.              
    30.                 newInstance = Instantiate(snowTile, camera.ScreenToWorldPoint(tempPos), Quaternion.identity);
    31.                 distanceNewtoOld = newInstance.transform.position.x - oldInstance.transform.position.x;
    32.              
    33.                 if(distanceNewtoOld > newInstance.renderer.bounds.size.x) {                  
    34.                     var nbToInit : int = (distanceNewtoOld-newInstance.renderer.bounds.size.x)/newInstance.renderer.bounds.size.x;
    35.                     Debug.Log(nbToInit);
    36.                  
    37.                     for(var k : int = 0; k<nbToInit; k++){
    38.                         var whereToAdd : Vector3 = Vector3(oldInstance.transform.position.x + 0.5*oldInstance.renderer.bounds.size.x,oldInstance.transform.position.y,oldInstance.transform.position.z);
    39.                         oldInstance = Instantiate(snowTile, whereToAdd, Quaternion.identity);
    40.                     }
    41.                     oldInstance = newInstance;
    42.                 }
    43.             }
    44.     }
    45.     transform.position += Vector3(speed * Time.deltaTime,0,0);
    46. }
    I finally understand the issue but I can't make it work, something must be wrong in my code but can't find out what.
     
    Last edited: Dec 29, 2014
  6. Arkolve

    Arkolve

    Joined:
    Dec 29, 2013
    Posts:
    20
    Maybe there is an other way to do it but I don't know how