Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DavidJares · Aug 22, 2019 at 03:09 PM · movementupdatetime.deltatimealignment

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 :)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DavidJares · Aug 22, 2019 at 09:35 PM 0
Share

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 ?!

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

238 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges