Quick Tip: Limit Framerate for Testing (Unity Tutorial)

  Рет қаралды 37,898

Code Monkey

Code Monkey

Күн бұрын

Пікірлер: 60
@izeckx
@izeckx 4 жыл бұрын
Wow, I just wanted to learn how to cap the framerate to 60 fps, but learned a LOT more than I was expecting. I wouldn't have thought about this until now. Thank you.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Thanks! I'm glad you found the video useful!
@DexterDLopez
@DexterDLopez 5 жыл бұрын
This was very useful! I've always had a low-end pc most of my life so I couldn't play most of the games I've always wanted. So now that I know how to test my game on different framerates, I can satisfy my players of both high-end and low-end pcs, thanks so much!
@rickloyd8208
@rickloyd8208 5 жыл бұрын
I was not aware of clamp, I was using combination of min/max functions. Also the way to simulate frame rate is awesome. As always thanks
@utkugokalp3179
@utkugokalp3179 5 жыл бұрын
Thanks :) Exactly what I needed for my current game
@IVANEZEDITIONS
@IVANEZEDITIONS 4 жыл бұрын
Thx u man!! You Saved my CPU!! My game was runing at 600FPS!!
@1nguoixauxi2
@1nguoixauxi2 3 жыл бұрын
will the Targetframerate reduce the works of the CPU and battery as well? or its just how the CPU limited the "displayed" image to us?
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
It limits the GPU which does indeed impact battery life, if you leave it unlimited and your phone is running at 200 fps it will drain a lot more battery than if you limit it to 60fps
@1nguoixauxi2
@1nguoixauxi2 3 жыл бұрын
@@CodeMonkeyUnity thank you, I'm looking for this solution long ago. For some non-action games, that would help alot. I would limited it to 27~31 only for mobile non-action game.
@Kaaxe
@Kaaxe Жыл бұрын
I need to find more info on this subject. My first built game is just entirely broken as all my code was tweaked with my 800fps in-editor game in mind
@Ulverbrook666
@Ulverbrook666 5 жыл бұрын
can u make a video about combining meshes and optimization ?
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Combining different meshes is extremely tricky. What exactly are you trying to accomplish? Check out my videos on how to make a mesh through code: kzbin.info/www/bejne/nZ7YeXyVrNiCnas kzbin.info/www/bejne/fHivZ4acm6mYjLc
@Ulverbrook666
@Ulverbrook666 5 жыл бұрын
@@CodeMonkeyUnity Thank you so much for ur answear. Well i have a problem with Batching (over 3000 batches) and the fps is good i think (between 75 and 150), it's a mobile game... i heard in some forums if i combine meshes i will reduce draw calls (correct me if am wrong) .. i did all the optimizations possible, like i maked already all the prefabes that will not move in the scene as a static prefabes that reduced a little bit the batches but still over 2000. any idea ?
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
The problem are the Draw Calls not the Batches. Batches are what Unity is grouping into a single draw call so you want as many things as possible to be batched so you have few Draw Calls. However at a certain point regardless of how much you optimize there are limits, having 2000 objects in your scene will be tough on Mobile no matter what. So look into how many objects you have and how you can reduce them.
@darenlim1584
@darenlim1584 4 жыл бұрын
I'm not sure if this is related, but spawned a question about how one would go about locking updates to a refresh rate of 60fps. For example a fighting game where the moves all are expected to startup, be active and end in set number of frames. How would one best go about doing so in Unity. Thanks in advance.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Set a simple float, modify it by Time.deltaTime and handle the logic with a sort of tick system Something like this kzbin.info/www/bejne/hHfZnpmlh9OUqLs
@darenlim1584
@darenlim1584 4 жыл бұрын
Thanks, I'll watch it a bit later. Initial thought, I wonder what happens if the framerate is slower than the desired tick rate. Say the games we desire updates at 60hz, but framerate can only hold say 45fps. I'm thinking the tick system would possibly update the elapsed tick but game would still lag. Unfamiliar if the tick system could be updated independent of framerate. Not sure why time and framerate updates needed to be tied together. I briefly explored fixed update but that wasn't reliable.
@grammarnazi1868
@grammarnazi1868 3 жыл бұрын
I did exactly this, but it's not doing anything. The stats window still shows the game running at 140+ FPS.
@nevimkde9537
@nevimkde9537 Жыл бұрын
Thank you a lot, but how would I handle collisions, because my character at lower framerate goes directly into tree tiles and then out of them like its noones business.
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
How are you handling collisions? If you're using a Rigidbody make sure you set Collision Detection to Continuous instead of Discrete If you're doing a manual OverlapPoint use a Raycast instead That way it won't just test the final point but every point in between
@nevimkde9537
@nevimkde9537 Жыл бұрын
@@CodeMonkeyUnity I am using rigidbody to handle collisions, but its set to continous already and rigidbody of the tilemap is set to static, but I am still able to walk through objects like lamp.
@kardokdelikaya9570
@kardokdelikaya9570 3 жыл бұрын
I got a void that i want to play 10 times. But when it's less than 30 fps it only plays 1 time. How can i fix this? (There is like 0.01 seconds between calls)
@bini6991
@bini6991 Жыл бұрын
Although it works fine in lower target framerate values, such as 10, it starts to deviate off the target the higher it is, 20 fps target gives me 21 - 22 fps, even higher like 100 fps target gives me 125 - 140 fps. Whats going on? Can I fix this?
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
That shouldn't happen, where are you seeing that framerate? I would assume you have an issue in how you are calculating framerate rather than targetFramerate not doing what it should. Look in the Unity Game window Stats, it should be fixed
@bini6991
@bini6991 Жыл бұрын
@@CodeMonkeyUnity Yeah, I'm using Unity's built in counter, that's the number it shows me. I suppose it's worth noting that I change the framerate cap within play mode through UI I made for the game, and don't set it beforehand. That must have something to do with it.
@sidbell929
@sidbell929 4 жыл бұрын
Can we bring it out to the inspector? If so, can we change it on the fly? Just came here from the Steam App - it's looking good! (I'll leave a review in a bit when I've clicked on everything :-) )
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Bring what to the inspector? Making a dynamic target framerate? I guess you could make a simple script with a public variable and on update set it to whatever value you have on the variable that you can then set in the inspector. I'm glad you like the Steam app! Thanks!
@user-ef6tm3ut4r
@user-ef6tm3ut4r 4 жыл бұрын
here it is :D you can modify the refresh rate on the inspector (it set the refresh rate for the text which means how many times the text will be updated each second in our case it's going to refresh 10 times each second 1 sec / 0.1 = 10) hope you get it :D private int frameCounter = 0; private float timeCounter = 0.0f; private float refreshRate = 0.1f; [SerializeField] private Text framerateText; void Update() { if (timeCounter
@sidbell929
@sidbell929 4 жыл бұрын
@@user-ef6tm3ut4r Thanks - I'll try this out soon :-)
@willpetillo1189
@willpetillo1189 4 жыл бұрын
Thanks! I am trying to fix a bug (navigation agents jittering at waypoints when using steering direction to control facing/moving) that is present on a tester's PC but not mine, which makes it really hard to troubleshoot. Hopefully this makes the bug easier to track down...or if not, to at least be able to rule out frame rate dependency.
@deraminator945
@deraminator945 3 жыл бұрын
Do I have to put that on every script? Or is it enough to just put it somewhere?
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
Just need to run the code once, could be anywhere, it will stay active until you change it to another value
@deraminator945
@deraminator945 3 жыл бұрын
@@CodeMonkeyUnity Thanks, I also found something interesting: On mobile platforms the default frame rate is due to the need to conserve battery power set to the default frame rate of 30 frames per second and for some reasons (in my case at least) Application.targetFrameRate = -1 won't change a thing even if the game could easily reach more than 60fps. So I found a script that makes it possible to bypass the default setting: using System.Collections; using System.Collections.Generic; using System.Threading; using UnityEngine; public class FixFPS : MonoBehaviour { [Header("Frame Settings")] int MaxRate = 9999; public float TargetFrameRate = 60.0f; float currentFrameTime; void Awake() { QualitySettings.vSyncCount = 0; Application.targetFrameRate = MaxRate; currentFrameTime = Time.realtimeSinceStartup; StartCoroutine("WaitForNextFrame"); } IEnumerator WaitForNextFrame() { while (true) { yield return new WaitForEndOfFrame(); currentFrameTime += 1.0f / TargetFrameRate; var t = Time.realtimeSinceStartup; var sleepTime = currentFrameTime - t - 0.01f; if (sleepTime > 0) Thread.Sleep((int)(sleepTime * 1000)); while (t < currentFrameTime) t = Time.realtimeSinceStartup; } } } Notice how the game kinda locks on to the setted fps instead of beeing close to the fps goal like in Application.targetFrameRate = 60; This works in Unity 2020.3.21f1 I hope this helps anyone :)
@BeerfootBandit
@BeerfootBandit 4 жыл бұрын
Thanks for the info. Question, if I limit the frame rate to something like 30(mobile) and a low end device can run at only 20, will it try to force itself to run at 30? Or will it run at any speed lower
@5ld734
@5ld734 3 жыл бұрын
It should run at any speed lower I believe
@MegaCX
@MegaCX 4 жыл бұрын
How can I do the opposite so that my game runs at more than 240 fps if the hardware is powerful enough?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
If you set it to -1 it will run unlimited as fast as possible
@darcking99
@darcking99 4 жыл бұрын
Application.targetFrameRate = 10; It does not work for me
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
How does it not work? It should force your game to run at that frame rate
@NowKnownMAWO
@NowKnownMAWO 2 жыл бұрын
Is it possible to limit the framerate of just one object?
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
No, framerate is global. Do you want to run an Update() less often? You can just use a basic timer
@NowKnownMAWO
@NowKnownMAWO 2 жыл бұрын
@@CodeMonkeyUnity I want to get an jitter/choppy animation by reducing the draw call/framerate but just for specific objects. The effect should look like the animations in spider-verse. Can I archive this by running the Update() less often? I mean, the object still need the same physical speed, just less draw calls...
@SDFTDusername
@SDFTDusername 4 жыл бұрын
thanks now that the fps limit is set to the monitor's max fps now the gpu wont have to calculate so much
@blitzingerdarkblaze7331
@blitzingerdarkblaze7331 5 жыл бұрын
Nice as always!
@christinedelire3974
@christinedelire3974 Жыл бұрын
Can't you just use the maximumTime.deltaTime instead of clamping it ?
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
What? There's no such thing as maximumTime.deltaTime
@cristiano4826
@cristiano4826 5 жыл бұрын
How to stick the stickygrenade into an enemy? (from ur Sticky Grenades tut☺️)
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
You would need to test for when the sticky grenade hits an enemy and then store that enemy as it's parent and move the sticky grenade position to be on top of the enemy. I may continue that series in the future.
@fritt_wastaken
@fritt_wastaken 2 жыл бұрын
why would you ever put any actual game logic in update? there's fixed update for that
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
Uh? FixedUpdate is useful when dealing with physics. There's tons of game logic that does not use physics in any way.
@pancitoalemanycartografo.1780
@pancitoalemanycartografo.1780 4 жыл бұрын
using System.Collections; using System.Collections.Generic; using UnityEngine; public class isMoving : MonoBehaviour { private bool isMoving; private void Awake() { IsMoving= true; Application.targetFrameRate = 10; } void Start() { } void Update() { if (IsMoving) { Vector3 targetPosition = new Vector3(70, 0); Vector3 moveDir = (targetPosition - transform.position).normalized; float moveSpeed = 200f; float moveAmount = moveSpeed * Time.deltaTime; moveAmount Mathf.Clamp(moveAmount, 0f, Vector3.Distance(transform.position, targetPosition)); transform.position += moveDir * moveAmount; if (Vector3.Distance(transform.position, targetPosition) < 1f) { IsMoving = false; } } } }
@zionen01
@zionen01 4 жыл бұрын
I'm thinking there has to be a better way than coding for each edge case that behaves differently due to FPS... This kind of sounds like a nightmare to maintain.
@willpetillo1189
@willpetillo1189 4 жыл бұрын
Yes, it's consistently writing code that is not frame rate dependent, which the video illustrated but did not state explicitly. The first version contained a defect, slowing down the frame rate for testing made the already-existing defect visible, and the change fixed it.
@bluemodize7718
@bluemodize7718 3 жыл бұрын
I want to limit the fps because of the coil wine
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
In order to prevent unlimited FPS you can also just enable VSync
@todadasch
@todadasch 3 жыл бұрын
40.
How to make a Mana Bar in the UI (Unity Tutorial for Beginners)
16:19
Гениальное изобретение из обычного стаканчика!
00:31
Лютая физика | Олимпиадная физика
Рет қаралды 4,8 МЛН
Trigger an Action after some Time (Unity Tutorial)
18:39
Code Monkey
Рет қаралды 61 М.
The MOST WANTED Unity Feature is FINALLY here!
12:15
Code Monkey
Рет қаралды 50 М.
SCRIPTABLE OBJECTS in Unity
8:57
Brackeys
Рет қаралды 1 МЛН
TOUCH CONTROLS in Unity!
16:09
Brackeys
Рет қаралды 1,3 МЛН
Simple Sound Manager (Unity Tutorial)
19:27
Code Monkey
Рет қаралды 125 М.
How to make a Clock in the UI (Unity Tutorial)
8:59
Code Monkey
Рет қаралды 32 М.
GENIUS Solution for HUGE WORLDS!
5:50
Code Monkey
Рет қаралды 17 М.
Unity Gaming Services: User Authentication with Username & Password
33:09
The Tales of Old
Рет қаралды 1,2 М.