Simple Challenge ? moving a row of Cubes in Updateloop - they dont align exactly and cause jittering spaces between them
Hello All, I need some help. I didn't ask a question for years, but now I don't know what is causing the problem.
I am trying to spawn simple cubes and move them along the X-Axis. I want them to be perfectly aligned next to each other. I am moving them during the update-loop with time.deltatime * velocity.
Whenever a time-interval is passed I spawn one. When spawning, in order to make up for the latency between the interval being passed and the frame being updated, I compute the frame-delay-delta between the timer and the update-loop-time. Then I nudge the spawned cube with that little detla*velocity into the direction it should go.
From my understanding the cubes should line up perfectly then and move together like butter.
However when I watch the result, the cubes are not perfectly alining. There are sometimes small gaps and sometimes there are small overlaps.
The positions of the cubes should be multiples of the velocity/spawninterval , but they are not. I am debugging them and the positions are not perfect multiples.
Can anybody please help ? Am I thinking wrong ? Am I missing something ? Here is the script I wrote for showing this.
Just add it to a gameobject and press play, choose a bright background and you will see the jitter sometimes. When you hit pause you can read the positions and can see that they are slightly different, which sometimes is too much and causes the gap problem .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DebugCubeSpawner : MonoBehaviour
{
public float spawnInterval = 1f;
public Vector3 Velocity = new Vector3(1f, 0f, 0f);
[SerializeField]
private Color textColor = Color.black;
[SerializeField]
private GUIStyle guiStyle;
public Vector3 guiOffset = new Vector3(0f, 0f, -0.6f);
List<GameObject> cubes = new List<GameObject>();
float timeCounter = 0f;
void Spawn(float frameDelay)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = transform.position;
cube.GetComponent<MeshRenderer>().material.color = Random.ColorHSV(0, 1);
cubes.Add(cube);
// We want to shift the cubes position for the delta-amount of
//time * velocity that the frame was too late after the actual spawn interval
Vector3 spawnShiftDistance = frameDelay * Velocity;
cube.transform.position += spawnShiftDistance;
Debug.Log("Spawn Position Shift X = " + spawnShiftDistance.x);
}
void Update()
{
timeCounter += Time.deltaTime;
if (timeCounter > spawnInterval)
{
// Whenever we are above the interval , we set it back one interval-length and spawn a cube
timeCounter -= spawnInterval;
Spawn(timeCounter);
}
foreach (GameObject cube in cubes)
{
// Move all cubes scaled with deltatime
Vector3 movement = Time.deltaTime * Velocity;
cube.transform.position += movement;
}
}
private void OnDrawGizmos()
{
// Draw a Label with position values of each cube on the cubes surfaces
guiStyle.normal.textColor = textColor;
foreach (GameObject cube in cubes)
{
string labeltext = "Position:\n" + cube.transform.position.x.ToString()+ "\n";
UnityEditor.Handles.Label(cube.transform.position + guiOffset, labeltext, guiStyle);
}
}
}
Thank you for any advice. Dave :)
As I found out, it works fine as long as I do pause the game-mode and do step by step clicking to the next frame. then everything lines up perfectly. so it only seems to matter when the game is really "running".
That doesn't solve anything but already gives a hint ?!
Your answer
Follow this Question
Related Questions
2D: Why does my character stutter if I add deltaTime to moveSpeed? 1 Answer
How move object on Bezier path? 0 Answers
GetKeyUp does not get called sometimes. 1 Answer
Character speed changes with screen 0 Answers
I need help with this script. Can't figure out my it isn't working.,Can't get this script to work 0 Answers