Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Bigproblem01 · Jan 20, 2015 at 09:01 PM · spritelagtranslatemovingjitter

Sprite movement is jittery/laggy

Hi guys,

My problem is - I'm moving a 64x64 sprite with a 2d collider on, using transform.translate and the faster I move it the laggier and jitterier it becomes.

My situation is - I literally have only a white background scene and a black square moving about randomly using Transform.translate, the square has a 2d collider on in order for players to tap it and that's all, there's nothing more on the scene or much else in code (except timers and trivial stuff). The square is moved by the below code and at random intervals, the directionX/directionY variables change:

 transform.Translate (directionX * moveSpeed * mySmoothDelta,
 directionY * moveSpeed * mySmoothDelta, 0);

where directionX is either 0, -1, or 1, movespeed is some float and mySmoothDelta is my custom variation of Unity's Time.smoothDeltaTime and still, when I build this on iPod 5 the square's movement is very laggy and jittery.

How it started is, at first I had just Time.deltaTime and it was jittery even in my editor. Then I removed Vsynch and it was better, then I tried Time.smoothDeltaTime and it was a bit better but I had no control over the number of previous frames to control the smoothness so I wrote a custom smoothDelta and it became a bit better again. My smoothDelta is this:

     frameTimesCounter += 1;
     frameTimesCounter = frameTimesCounter % 60;
     frameTimesArray[frameTimesCounter] = Time.deltaTime;
     frameTimesSum = 0;
     for (int i = 0; i < frameTimesArray.Length; i++) 
     {
         frameTimesSum += frameTimesArray[i];
     }
     mySmoothDelta = frameTimesSum/60;

now It runs almost perfectly in my editor if I turn off Vsync and not restrict the framerate via Application.targetFrameRate but if I build it on an iOS device, it's just beyond bad, and the higher the movement speed the worse it gets. Another problem is that because it lags it also fails to detect whether the square was tapped or not. I also have an iPad and iPhone 4 (weaker than iPod 5) but haven't build on those yet, though i'm sure i'll get the same result. Before I built it, I also experimented with putting a rigidbody on my sprite and moving it with a velocity instead but I got the same result as with Time.deltaTime so I proceeded with solutions on how to make translate better and after searching online came to the custom delta smooth.

I tried a lot of things and my online search provides no more leads, if anyone can make sense of this I'd highly appreciate it.

All I want is for this square to move as smoothly as possible, as this is one of the main goals of this project.

Thank you all in advance.

Comment
Add comment · Show 9
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 Superrodan · Jan 20, 2015 at 09:14 PM 0
Share

If you disable the collider but keep everything else the same does the lag go away?

Additionally, is it possible the "random variables" are too short and the square is constantly changing directions?

Lastly, I have bever used Transform.Translate before but it seems like it requires a Vector3 as its first input. If the Vector3 you're trying to go for is this:

 (directionX * moveSpeed * mySmoothDelta, directionY * moveSpeed * mySmoothDelta, 0)

then perhaps it might work like this:

 Vector3 tempDirection = new Vector3(directionX * moveSpeed * mySmoothDelta,
 directionY * moveSpeed * mySmoothDelta, 0);
 transform.Translate(tempDirection);
 
avatar image Eric5h5 · Jan 20, 2015 at 09:52 PM 0
Share

Are you using FixedUpdate? If so, change that to Update. Any moving collider should have a rigidbody regardless of how you move it, because otherwise the physics engine has to rebuild the colliders. @Superrodan: that's not how Translate works. It's just relative movement; look it up in the docs.

avatar image Bigproblem01 · Jan 20, 2015 at 10:07 PM 0
Share

@Superrodan,

I haven't tried building without the collider (can't build now since I have apple at my office) but when it was jittery in editor, I tried disabling the collider there and it didn't really do much.

Random time intervals are not too short and it is clearly visible when the square changes the direction.

why would one store Vector3 in a tempDirection when it can be directly passed like this?

  transform.Translate (directionX * moveSpeed * mySmoothDelta,
  directionY * moveSpeed * mySmoothDelta, 0);

@Eric5h5 No, I'm using regular Update(). Are you talking about the fact that if it's not rigidbody it redraws all the colliders in the scene on every frame? I've heard of this but since I only have one square on the entire scene and moving with velocity didn't help I didn't put the rigidbody on since I didn't see the need for it. So you're saying I should just put the rigidbody2d component anyway despite the fact that I'm neither using velocity nor any rigidbody component parameters? (should I put all of them on 0 then?)

avatar image Superrodan · Jan 20, 2015 at 10:30 PM 0
Share

I apologize for my misunderstanding of Translate. I misread the documentation.

avatar image Eric5h5 · Jan 20, 2015 at 10:35 PM 0
Share

On the desktop, you should turn on vsync for best results, and don't use Application.targetFrameRate. On iOS, it's impossible to disable vsync, and ins$$anonymous$$d targetFrameRate is used to set whether you're using 30 or 60fps, though ideally you should use 60. But yes, you should always use a rigidbody when moving a collider, under any circumstances. Not that it would make a noticeable difference in the case you describe, but you should make a habit of it. Also I wouldn't bother with any kind of smoothed deltaTime; Time.deltaTime is enough. Seems like fiddling with it would lead to inaccuracies.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bigproblem01 · Jan 21, 2015 at 12:59 PM

I tried using Application.targetFrameRate = 60 this morning and that appears to have fixed the issue. Turns out iOS always defaults to 30 frames per second because when on 60 fps, if there's a drop in frame rate it's very noticeable... anyway, the issue's fixed.

Comment
Add comment · Show 1 · 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 dude4004 · May 20, 2016 at 02:55 PM 0
Share

It fixed my trouble. I had some very little lag when moving 2d sprite and when I've used this line Application.targetFrameRate = 60;

it did the trick. I'll test this out on Android or IOS to see if still work.

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

28 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

Related Questions

Pixelated sprite when changing position 0 Answers

Lag in scene view 3 Answers

Moving 2D collider and lag 1 Answer

Jitter on object falling down on PC and android both. 0 Answers

Moving sphere tremble in moving camera view[Solved] 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