Well I know animator system in Unity is very useful for many things but I find myself having my own state machine in the code and somehow the state machine that the animator provides is not accurate due to blend time maybe I dont know, so I find often my character being in state 2 in the animator state machine and state 0 in my own state machine and its crazy as I cant seem to make it match in limit cases when its about to end the action. So I would like to know if there is a way to just say with code "play this animation from frame A to frame B" and loop it or not loop, that would be much better for me, of course I will lose all the blend features but really I will be better, all I can fin in the docs refer to using the animator so far. Thanks a lot for any help regarding this =)
Edit: Forgot to add that I found Animation.Play but apparently this only play animation saved on separate files like when we use the Animation timeline to record a certain motion, but I have a character with a lot of frames there and I dont think it serves in this case (or in my tests it never finds the specified animation at least, maybe i miss somethnig)
before anything, you can your own state machine with Animator. in fact, you don't need to two different state machine for your purposes. (Watch this video here)
and about your question, I found a good answer here: Link
From what I've seen you can either do AnimationState.normalizedTime
which returns the progress of an animation on a scale of 0 to 1. So if
you have a 30 frame animation and you want frame 15, you can do if
(normalizedTime == 0.5)* Or the better and more reliable option is to
use AnimationEvents that fire on specific frames.
*You won't really be able to do this if we're dealing in floats--You can only get the approximate value or you'll have to check greater
then or equal to 0.4 AND less then or equal to 0.6 because the
animation could go faster than the current frame rate or the
normalized time value could be 0.50000001 because of the nature of
float values.
Related
I am building a big world (1000+ game objects). It takes 2s on the device. I am doing it in the background (the world is hidden until fully loaded). I have figured out that I can maintain the FPS of the active scene by switching to coroutines and making yield return null;.
The main issue is when to fire yield return null;. If I do it too often, then it will slow down the loading. If I do it too rarely, it will reduce FPS on low-end devices.
My idea is simple to detect how much milliseconds have passed since the start of the frame and fire yield return null; just after I have spent for example 12ms (to leave some time to render everything else and maintain something near 60FPS).
However, I can not find an efficient way of getting the time since frame start? Unity should have something as the async loading of assets seems to consume just right time to maintain stable FPS.
DMGregory has given the great and right answer on GameDev StackExchange.
You can use and assign Time.time to some variable (example lastFrameTime) and in next frame check if Time.time - lastFrameTime > amountOfTimeToRefresh...
Or Time.delta time assign to lastFrameTime and do the same if...
and if that condition is true do whatever you want and reset lastFrameTime to 0
Hope it helps...
I'm going to create an endless runner in Unity and I was wondering if I should move the player or the scenario during the runs.
The most obvious answer sounds like "the player" because you are moving fewer objects but... does the performance get affected if the size of the scenes is too large? I don't think so but my real worry is about the coordinate:
What happens if the player runs so far away that the coordinates can't fit in a float variable? I think that the transform component uses a Vector3 to store the coordinates and this Vector3 uses float variables (with a limit of +3.4E+38) for each of the coordinates.
Thank you for your answers in advance,
Guillem Poy
Even if I don't know what happens if the value of the coordenates exceeds the capacity of a float variable (I think that then the number simply will be wrong) doing some calculus...
Even if the player is moving 1000 units per second, to create that problem, the player should be playing during 9.4+E32 hours (2.5+E29 years). I think that nobody is going to play this much.
So, the best option I think that is to move the player.
I'm making my first game! It's a very simple 2D game, just a character that can run around and jump on blocks.
My timer's interval is 1, each tick it checks for a collision between the player and the blocks. For some reason the only block that the character doesn't get "jumpy" on is the last block that it's collision is checked. Maybe Multi-Threading will work?
This is my first game so sorry if the code is messy. I'll divide my code to two (Using pastebin because it's long, well at least for me):
The whole game code: http://pastebin.com/GX4PtUuL
What I think is needed: http://pastebin.com/GEBEinm8
If you need me to tell you more feel free to ask. Please remember that I'm not a 'high level' programmer, so I will not understand everything.
Thanks in advance!
OK, I have now looked at your code.
It seems to be basically OK; especially collision checking in the Tick event is correct since you are using a gravitational force that works in time.
It works OK, but only if the timer speed is slow enough, I found around 100ms to work fine..
Here is tip that should help you further: The pixels, locations etc are all integers but player position and speed/force really should be stored and calculated as floats so you can fine tune things, especially the speed.
Not sure if this is a Farseer thing to do with inertia, or if it's my code, but I've simplified the code quite a bit and I can't find it.
Scenario: I've got a Body, with a Mass of 10(kg I assume). I use ApplyLinearImpulse and I scoot the object to the right using a vector like (1,0) and constant like 5.
Problem: It does move to the right, but it seems to be capped. The LinearVelocity property does go up as I increase the value fed to ApplyLinearImpulse, but the actual change in Position does not. As soon as I call world.step(msDelta), the LinearVelocity drops back to some tiny value.
Am I doing this wrong, or is there an internal cap based on my mass?
There is a maximum movement limit of 2.0 units per time step, set in the file b2Settings.h in the Box2D source code. You can change this value (b2_maxTranslation) if you need to, just be aware that the values in this file are tuned to work well together so you may get other problems if you change it too much.
Note that this is a #define'd constant used throughout Box2D so you'll need to recompile the library itself to make the change take effect fully. I don't know enough about Farseer to tell you whether this is easy or not :)
Generally if you feel the need to change this value you might like to first consider scaling all your physics dimensions down so that your bodies don't need to move faster than 2 physics units per time step.
You might be interested in some other common 'gotchas' here: http://www.iforce2d.net/b2dtut/gotchas
I'm coding a server for a multi-player RPG, and I'm currently struggling with implementing a sight range. Since some maps are rather large, I have to limit what the client sees. My approach:
If I get new coordinates from the client, I save them as the destination, together with a move start time. Once every x ms I go through all creatures in the world, and update their current position, after saving the position they were at the last time I've updated them. Basically I calculate the new position, based on move start time and speed, and write those in the current position variables, while saving the new start time. Once this update is done, I'm going through all creatures which moved, aka those who have a different position than at the last update. In a sub-loop I go through all creatures/clients again, to check if I have to notify them about a (dis)appearing creature. At the moment I'm running this update every 100ms.
This approach is working, but I have a feeling it's not the best way to do this. And I'm not sure what will happen once I have a few thousand creatures (players, monster, etc) in the world, which have to be updated and checked.
Since I weren't able to find resources about this particular problem, I'm asking here.
Is this approach okay? Will I run into problems soon? What's the standard to do this? What's the best way?
Eric Lippert had a really good series of posts on shadowcasting that might be helpful in approaching/solving this.
You may want to consider using quadtrees to split the game world into sections based on the areas that player characters can see. Then you don't need to loop over every creature in the game all the time; you only need to loop over the ones within the section that the player character in question is located in, and any adjacent ones in case something crossed the boundary.
I haven't done this sort of coding personally myself, but I did work with someone who did this in a space combat game for which I was developing a GUI!