How to move a GameObject one after the other without having any gaps?
I'm trying to build a clone to this game. (http://gprime.net/game.php/copter), but I'm having trouble creating the walls.
I have two groups of 30 GameObjects. One for the top wall and another group for the bottom wall. Like so:
Each one has a script that moves the wall to the left, Here is the script:
public static float lvl = 0f;
private void Update()
{
float amtToMove = Time.deltaTime * LevelManager.Instance.wallSpeed;
if(!MasterManager.Instance.gameOver)
transform.Translate(Vector3.left * amtToMove);
if(transform.localPosition.x < 0)
Reset();
}
public void Reset()
{
lvl += 0.09f;
transform.localPosition = new Vector3(30f, transform.localPosition.y + Mathf.Sin(lvl), 0);
}
As the game continues LevelManager.Instance.wallSpeed changes value.
The problem with this, is that eventually it "breaks" and leaves a gap between each wall. Like so:
Is there a fix for this? I don't quite understand what I'm doing wrong.
Answer by eses · Sep 12, 2018 at 06:03 PM
Hi @nyanku
One solution could be like this:
If your wall blocks move towards left, check when the leftmost wall block removes itself.
When this happens, create a new block. New block should be placed relative to first block on right. If all your blocks are 1m wide, then place the new piece to rightMostPiece.transform.position + Vector3.right (one unit from it's position). This way I think it doesn't matter if blocks are moving faster than default speed, new block will just line up itself with the previously newest block.
Your answer
Follow this Question
Related Questions
Rigidbody2D.MovePosition() doesn't do anything (but animations and transform.Translate() works) 0 Answers
2D Using C# moving game object back and forth and random stop in between - all in x-axis 2 Answers
transform.translate limits 1 Answer
Diagonal character movement 0 Answers
Why doesn't the collider work? 1 Answer