Instantiate GameObject on every touch position (fluidity issue)

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 :

#pragma strict

var test : GameObject;
private var touchID : int = -1;
function FixedUpdate () {
    var count = Input.touchCount;
  
    for(var i : int = 0; i < count; i++)
    {
        var touch : Touch = Input.GetTouch(i);
      
        if ( touch.phase == TouchPhase.Began ) {
                touchID = touch.fingerId;
            } else if (touchID == touch.fingerId) {
                var tempPos : Vector3 = touch.position;
                tempPos.z = 10;
                var instance : GameObject = Instantiate(test, camera.ScreenToWorldPoint(tempPos), Quaternion.identity);
            }

Any suggestions to make this script stronger please ?

Maybe keep a running list of the current object created, and the last object created. If distance between greater than x, Instatiate another between.

1 Like

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…

@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.

I tried something like that (a rough draft) :

#pragma strict
var speed : int = 50;
var snowTile : GameObject;

private var touchID : int = -1;
private var oldInstance : GameObject = null;
private var newInstance : GameObject = null;

function Start() {
  
}
function FixedUpdate () {
    var count = Input.touchCount;
    var distanceNewtoOld : float;
  
    for(var i : int = 0; i < count; i++) {
        var touch : Touch = Input.GetTouch(i);
      
        if ( touch.phase == TouchPhase.Began ) {
                touchID = touch.fingerId;
              
                var tempPos : Vector3 = touch.position;
                tempPos.z = 10;
              
                oldInstance = Instantiate(snowTile, camera.ScreenToWorldPoint(tempPos), Quaternion.identity);
            } else if (touchID == touch.fingerId) {
                tempPos = touch.position;
                tempPos.z = 10;
              
                newInstance = Instantiate(snowTile, camera.ScreenToWorldPoint(tempPos), Quaternion.identity);
                distanceNewtoOld = newInstance.transform.position.x - oldInstance.transform.position.x;
              
                if(distanceNewtoOld > newInstance.renderer.bounds.size.x) {                  
                    var nbToInit : int = (distanceNewtoOld-newInstance.renderer.bounds.size.x)/newInstance.renderer.bounds.size.x;
                    Debug.Log(nbToInit);
                  
                    for(var k : int = 0; k<nbToInit; k++){
                        var whereToAdd : Vector3 = Vector3(oldInstance.transform.position.x + 0.5*oldInstance.renderer.bounds.size.x,oldInstance.transform.position.y,oldInstance.transform.position.z);
                        oldInstance = Instantiate(snowTile, whereToAdd, Quaternion.identity);
                    }
                    oldInstance = newInstance;
                }
            }
    }
    transform.position += Vector3(speed * Time.deltaTime,0,0);
}

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.

Maybe there is an other way to do it but I don’t know how