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 /
avatar image
0
Question by $$anonymous$$ · Nov 28, 2017 at 06:49 PM · updatelagframerateupdate problemlaggy

Update function unstable frame rate

Hi. i'm doing a simple translation of a 2D object using transform.Translate in Update function, but the movement is not occurring smoothly and i'm having little lags. I know that Update funciton is unstable with the frame rate update and i'm already using Time.deltaTime. The code part looks like this:

transform.Translate(speed * Time.deltaTime, 0, 0);

Someone know how to solve this and get a smooth movement?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dray · Nov 28, 2017 at 06:59 PM

FixedUpdate() and Time.fixedDeltaTime is what you need for high accuracy:

 void FixedUpdate() {
     transform.Translate(speed * Time.fixedDeltaTime, 0, 0);
 }

To explain this real quick; Unity runs two parallel update loops: one for the rendering that by its nature takes a random amount of time and one with a fixed timespan for the physical calculations in the game.


The reason Unity is doing that is simply that running the physical part of a game depending on that random "time"-value from the rendering step would produce unpredictable deviations in the small digits that lead to weird behaviours like what you are experiencing.


If you run the main loop of your game logic always with the same timestep, the results of physics interacting with each other in any way will always be the same with a probability of 100%, thats why unity provides the FixedUpdate function.

Comment
Add comment · Show 10 · Share
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 $$anonymous$$ · Nov 28, 2017 at 07:21 PM 0
Share

Still lagging man. $$anonymous$$y game object hasn't Rigidbody2D attached, i think i don't need FixedUpdate(). I've tried put the code in FixedUpdate() with transform.Translate(speed Time.fixedDeltaTime, 0, 0); and hasn't worked, I've tried only to use transform.Translate(speed Time.fixedDeltaTime, 0, 0); on Update() and hasn't worked too.

Have you an opinion about this?

Thanks anyway!

avatar image Dray · Nov 28, 2017 at 09:49 PM 0
Share

That's pretty weird, can you post the code of that script?

avatar image $$anonymous$$ Dray · Nov 30, 2017 at 11:41 PM 0
Share

This is my script:

public class ScrollRoad : $$anonymous$$onoBehaviour {

 public float speed;

 void Update () {
     transform.Translate (speed * Time.fixedDeltaTime, 0, 0);
 }

}

avatar image Dray $$anonymous$$ · Dec 01, 2017 at 08:03 AM 0
Share

Oh, don't use fixedDeltaTime in the normal Update loop, it's Time.deltaTime for the normal Update and Time.fixedDeltaTime for the FixedUpdate, sry if that wasn't clear but I guess that's not the issue since you had that problem allready when you added that :/ The only other Idea I got would be checking if other scripts influence the same object as @JonPQ suggested

Show more comments
avatar image JonPQ · Nov 28, 2017 at 10:21 PM 0
Share

First check your FPS.... enable "stats" window. is your FPS stable ? If your FPS is fast and stable, * TimeDelta should be good enough.

If your FPS is choppy, lots of things it could be.... make sure you are not logging a bunch of debug logs to the console... that can make things run slow and choppy. You can turn them off temporarily under PlayerSettings->other settings.

Also... is this the only thing going on in the scene ? are you rendering too much... or too many transparencies? or instantiating objects while the gameplay is happening? try opening Window->profiler and look out for big spikes.... if you have a lot of spikes, pause the game and see what they are.... scripts or rendering....

avatar image $$anonymous$$ JonPQ · Nov 30, 2017 at 11:36 PM 0
Share

The FPS is around 68-70

avatar image JonPQ $$anonymous$$ · Dec 01, 2017 at 05:49 PM 0
Share

Another thought.... Watch the movement of your object in the scene view... is it smooth there ? I once thought something wasn't smooth when it was actually fine, it was the camera that was following the target object that wasn't smooth. So make sure that the camera position update, happens after the object position update (consistently) If object updates in fixed update, and camera moves in Update, then you will get hitches and discrepancies. If you have something setup like this... the easy way is to turn off camera Update() function... and call the function from the end of the character position update code.

Show more comments
avatar image
0

Answer by pako · Dec 05, 2017 at 09:38 PM

Your movement method should present no problem with unstable FPS. It's fine as it is: transform.Translate(speed * Time.deltaTime, 0, 0);

If you use the Instantiate ()/Destroy() methods too much, you are actually asking for unstable/low FPS. When you call Destroy() the garbage collector will kick-in at some point, and that's not good. Also, Instantiate() is rather heavy on performance. So, instead of Instantiate()/Destroy create a pool of objects and use GameObject.SetActive(true/false). You should really be using a Pool Manager for this, and there are even free ones in the Asset Store. Typically, Pool Managers use Spawn()/Unspawn() methods that utilize GameObject.SetActive(true/false) behind the scenes. https://assetstore.unity.com/search?q=pool&q=manager

Comment
Add comment · Show 8 · Share
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 $$anonymous$$ · Dec 05, 2017 at 09:52 PM 0
Share

I'm using pool already. I believe I'm doing nothing wrong. This is my pool scripts:

public class RoadInstanciator : $$anonymous$$onoBehaviour {

 public float instanciationTime;
 public float disableTime;
 public static float disableTimeStatic;
 public GameObject road;

 public int pooledAmount;
 List<GameObject> roads;

 void Start(){
     disableTimeStatic = disableTime;

     roads = new List<GameObject> ();
     for (int i = 0; i < pooledAmount; i++) {
         GameObject obj = (GameObject) Instantiate (road);
         obj.SetActive (false);
         roads.Add (obj);
     }

     InvokeRepeating ("InstanciateRoad", instanciationTime, instanciationTime);
 }

 void InstanciateRoad(){
     for(int i = 0; i < roads.Count; i++){
         if (!roads [i].activeInHierarchy) {
             roads [i].transform.position = transform.position;
             roads [i].transform.rotation = transform.rotation;
             roads [i].SetActive (true);
             break;
         }
     }
 }

}

public class RoadDisable : $$anonymous$$onoBehaviour {

 void OnEnable(){
     Invoke ("Disable", RoadInstanciator.disableTimeStatic);
 }

 void Disable(){
     gameObject.SetActive (false);
 }

 void OnDisable(){
     CancelInvoke ();
 }

}

avatar image pako $$anonymous$$ · Dec 05, 2017 at 10:30 PM 0
Share

You might find the source of the problem with the Profiler. Start the Profiler (Window/Profiler), start Play mode, and check the Profiler for spikes. Play around with the Profiler's settings and see if you get any clues from there. BTW, I noticed from your previous comments that you don't have a Rigidbody2D attached. If you have any Collider2D on you GO, and no Rigidbody2D, Unity internally attaches the Collider2D to a single hidden Static Rigidbody 2D component. I don't know if this is the source of your problem, but it's something to keep in $$anonymous$$d. Check out "Body Type: Static" in https://docs.unity3d.com/$$anonymous$$anual/class-Rigidbody2D.html In any case, it would be better to attach a Rigidbody2D to your GO, and enable is$$anonymous$$inematic.

avatar image $$anonymous$$ pako · Dec 05, 2017 at 11:20 PM 0
Share

Ok @pako I'll check, thanks!

Show more comments
avatar image pako $$anonymous$$ · Dec 05, 2017 at 10:31 PM 0
Share

Also, your Pool code seems fine.

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

74 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

Related Questions

Lagspike when using instantiate in coorutine 0 Answers

Unity lagging after update? 2 Answers

What is the download size of updating from version 2019.3 to 2019.4.1f1? 0 Answers

Semaphore.WaitForSignal is causing so much lag on Android!!! Help!!! 1 Answer

iOS Lag From Updating Score 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