- Home /
Horrible lag in 2d roguelike character movement
I'm using a modified version of the game from the 2d Roguelike Tutorial . I've made only slight modifications to the original scripts. I've mainly added new UI elements and scripts to handle those UI elements.
I'm having issues with character movement. In the editor, the characters will mostly move fine, but when I build, all of the characters move, VERY slowly. It takes roughly two full seconds for a character to move into position. Everything else seems to run at full speed, but the cpu usage in the build is much higher than in the editor.
Also, even in the editor, there are still sometimes issues with the player character kind of weaving around and not staying square on a tile, so that if you walk by food it will pick up food on both tiles, etc.
I don't know why any of this happening. There doesn't seem to be any bugs in my code that would cause this, and I don't know why movement would be so much slower in the build rather than the editor, or why cpu usage would jump. Any ideas about what might be causing this?
https://youtu.be/RKtuwGfCxGk here's an example of the slow movement in the build. The character also doesn't seem to always stay within the bounds of the tiles and weaves just as it did in the editor, only much slower.
https://youtu.be/6RyJNPqkNq4 and here's an example of the the weaving the player does in the editor.
The character also glides from position to position without stop when holding the button down. And when checking the completed project today, the character seems to stop briefly before going on to the next tile when the button is held down. This behavior seems somewhat random, because even with nothing changed, the player will sometimes move normally.
You can see more about the project at http://www.fragmentalstew.com/
This is a demonstration of the game: https://youtu.be/FyUlUlBhzqs
The license of the Roguelike tutorial prevents me from redistributing the project, but I may be able to give you access to to the project if you message or email me.
So....this bug is back, but in a much later version, after I've made a lot of changes. Except this time the fix I did previously doesn't work. Additionally, character movement seems fine in the editor, only not in the build. :/
Answer by Fragmental · Nov 12, 2015 at 12:24 AM
So, I was trying to figure out what was wrong with the completed folder in my project, because it wasn't working right. I had changed the name of a script and made a few changes, then copied the original script back but forgot to change a reference. So I changed the reference, and the completed scene still had issues with movement. So I tried copying an unaltered version of the completed folder over, and every time I did, that reference reset itself, which made no sense since the prefab with the reference was coming from the completed folder I was copying over.
I have a question about it here: http://answers.unity3d.com/questions/1096799/copying-assets-from-one-project-to-another-creates.html
This made me think there could be something wrong with my project. I've been working on the project for 6 months or more and in that time I've had some computer crashes and unity crashes and other issues. So I exported my modified version, copied over the TagManager.Asset file, created a new project and then imported it in. This seems to have fixed the movement issue!
I have no idea what the cause was or if there was any other way to fix it. It may have even had something to do with the Unity upgrades I installed since I started. I have no idea. Either way it seems to be working now. After the export, there was a problem with lines across the board map but it seems to have resolved itself on its own.
Thanks @o-super, you helped me find a solution to this. At first I thought it had something to do with the massive changes I had made in the project, but we both seemed to experience the same issue. I think it was just some sort of buggy weirdness.
Awesome, I'm glad you did solve the problem. $$anonymous$$eep going on unity, the community seems to be great.
I just wanted to say thank you :) I've got exactly the same error (your video made me sure that its not only me :) ) I checked all code in project without success.. Then I found this answer and it worked! :D
Answer by o-super · Nov 11, 2015 at 01:20 AM
Hello FragMental,
I had the exact same issue tonight while reaching the step 11 of the tutorial. I tried to build the game for windows and my unity stoped working. I killed the process and launched a new instance of unity.
The only loss I had in the process was my "Main Camera" parameters and the Player who disapeared from my hierarchy. I linked back my GameManager prefabs to the Main Camera and put my Player prebad in the hierarchi and here we go, problem solved.
The only one advise I could give you is to try to create a new scene, remake the steps I did and try.
Your characters in the game were moving slowly? Your player was weaving and not staying inside the tiles?
Building works fine for me, it's just the performance after the game is built that is an issue. There are a few issues with movement even in the editor, however.
I have a lot of UI and code additions so redoing the whole thing would be a big ordeal. Any one of the videos will show you what it looks like. You did give me an idea though. I wonder if I could prefab my UI elements and then import them back into a new scene to skip a lot of the work. Then I could rebuild the scene and see if it still has the same performance issues. That's still a lot of work, even if I can do that. I don't know if will, but it might be worth a try, since I'm currently completely stuck.
Hey back,
Yes my characters were moving slowly and the player was weaving and not staying inside the tiles. The entire application was running slow and because of this I could move my player before the end of his previous move causing the player to end on a bad location.
In fact I reproduced this problem multiple times, it always happens when I test the game in the Unity IDE after recovering my windows session from a SLEEP mode.
To be clear, If I turn off my PC in sleep mode with Unity open, the next time I turn on my PC I will have to close my Unity and start a new one to make it work properly.
Another thing, I did another mistake that could be related to the problem (not sure). I'm sharing this just in case, it might help.
$$anonymous$$y game was instanciating multiple scenes at the same time causing the slow effect. To be sure you're not instanciating multiple scenes make sure you did remove the Game $$anonymous$$anager fromyour Hierarchy (sometimes hidden under $$anonymous$$ain Camera) and that your game manager prefab is attached to your Loader script in the main camera.
Lemme know, I really want you to go threw that problem.
That would be a good reason for issues.
No gamemanager object in my scene here. Just the loader script. I did notice something different about the completed scene and my scene though. The completed scene loads the soundmanager in the loader script, and my soundmanager is in the scene. I don't think it really matters either way for the soundmanager though.
Answer by Sasune_Fair · Sep 22, 2019 at 04:42 AM
Hey everyone,
I know it's been years but I thought I'd leave my experiences here for any future dwellers.
Basically I had a similar issue as stated by @Fragmental where my character would move super slowly (about 3 seconds per step), and if I input move commands while moving, the character would then do the aforementioned "weaving".
I downloaded a run time Log Viewer by Dream Makers Group to dive into what was really going on.
So basically what I found was that the game was running at 600 fps. It was running at 600 fps as a result of selecting the build option 'Fastest' when building the game. Not sure why that happens though. Anyway, setting the build options to 'Beautiful' made the game run at 60fps, and then everything worked fine.
The reason why things didn't work as well at 600 fps can be described by the following post: https://answers.unity.com/questions/10993/whats-the-difference-between-update-and-fixedupdat.html?_ga=2.82499414.1351522315.1569119747-984558385.1533734503
If you ever stumble across this, do try this solution :) And go Unity! You guys rock.
For any current and future dwellers here I strongly recommend reading the above post about the difference between update and fixed update!
Answer by DavidPfeiffer54 · May 26, 2020 at 04:15 PM
I am encountering the same problem in the editor even when running the fresh out the box "completed project". (like stated above, when built/ran it seems to run fine)
I believe rb2D.MovePosition(newPostion) in the SmoothMovement function in MovingObject seems to be the problem. The position doesnt seem to be updated every time SmoothMovement is called. based on the docs, it says that the 'actual position change will only occur during the next physics update'. Is it possible that the physics update isnt happening every call when ran in the editor?
I fixed this by changing
rb2D.MovePosition(newPostion);
to
rb2D.transform.position=newPostion;
This sounds really promising for a permanent fix, but if this was the problem all along, I don't know why moving it to a new project would have fixed it for me, or why limiting the fps fixed it for someone else.
Thanks for a tip! The problem is that we update newPosition every frame but move object by physics which uses FixedUpdate at it's own framerate. Your fix is working, but if we still want use physics for movement we can change:
Vector3 newPostion = Vector3.MoveTowards(rb2D.position, end, inverseMoveTime * Time.deltaTime);
to
Vector3 newPostion = Vector3.MoveTowards(rb2D.position, end, inverseMoveTime * Time.fixedDeltaTime);
This is almost always an issue of FixedUpdate vs Update and all the intricacies to do with Time therein! Nice find.
Answer by dshaw002 · Jan 05, 2021 at 06:03 AM
The cause of the problem is the FPS going too high during the build. To fix it, create a new script called TargetFrameRate and attach it to your GameManager prefab:
TargetFrameRate.cs
using UnityEngine;
using System.Collections;
public class TargetFrameRate : MonoBehaviour {
public int target = 60;
void Start()
{
QualitySettings.vSyncCount = 0;
}
void Update()
{
if (target != Application.targetFrameRate)
{
Application.targetFrameRate = target;
}
}
}
This will basically force the application into 60fps, resulting in smooth scrolling. Hopefully this can be marked as the correct answer @Fragmental.
Your answer
Follow this Question
Related Questions
How to check if mouse is above or below player? 2 Answers
How do I make my character move on the Z axis on 2d rigidbody? 0 Answers
How to apply light on post processing volume 0 Answers
I have an animaiton problem about sprite loading. 0 Answers
How can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers