I am currently working on a mobile game and I can't help but notice that whenever an object (an obstacle in my case) is instantiated or destroyed, I get a sudden FPS drop which is critical for my gameplay.
To help give an idea, I instantiate obstacles on top of my screen every 1.5 seconds, then I scroll them down. If the obstacles reaches the bottom of the screen already, I destroy them to prevent memory leaks/waste.
I'm still pretty new to Unity development. Am I on the right track though? What is a better solution to prevent this sudden frame rate drop?
Do you have any big / nested loops or complex processes going off as part of instantiation (Look at your awake/start methods)?
Regardless, look into object pooling as a better method to handle this type of thing.
For a basic example, instead of creating/destroying projectiles of a gun every time it's used, give that gun a "projectile pool" that creates n projectiles when the level loads. Then, when shooting, just set the projectile's position back to the gun and set the projectile as active. After impact, have the projectile deactivate (or after a few seconds if nothing is hit).
Related
I have recently begun working on a game for a uni project and will visit this site probably a lot while encountering problems. This time it's a small but annoying problem.
I am doing a 2D sidecroller so I used the template for it to get me started. I already put in the input for attacking and want that the character stops moving when he does the attack animation. On the ground it works wonderfully, but while airborne the character doesn't completely stop in the air, but is slowly decending during the animation. And I want that the character stops completely.
Here is the Jump Handling from the 2d sidescroller template.
Here is the event tick and where the animation update is called (Ignore the attack counter stuff, doesn't influence)
Here is the Animation update code and the "Stop Movement Immediately" node. It has to be said that I am doing what I learned from a tutorial.
As the name implies, the node should stop movement, which is does in the X Axis, but not completely in the Z Axis. I tried it by saving the location of the Player Character when he attacks while in the air but either I couldn't get the code right or it doesn't work like that.
I would be thankful for advice with this (rather petty) detail.
If your problem is strictly while falling, try setting the gravity to 0 while attacking, create an Event in the animation that fires up when it's finished and then capture it to set the gravity back to the original value.
I'm currently just trying to learn to use the animator within Unity, I'm very in-exp at animation and don't understand it even in the editor as I focus on programming/scripting.
I have an animation and the states for the animations as-well as the conditions all working perfectly however the animation check for the next state is way to slow. I've tried changing the speed of the actual state but it speeds the animation up and makes it look like my character is walking insanely fast.
I've tried messing around with the frames, making them over a longer time period and making the speed of the state faster however it seems to counter act each other, when I make it longer frames the pace of the animation is slow and then when I make the speed of the state go quicker it just makes the frames tick faster making the animation faster.
What I believe is happening is that the check for the next state of animation is happening once the full animation has been played. However what I need is the check to be happening constantly (as if frame by frame of the unity game not the animation).
Any advice would be great, I've tried using youtube to solve this before coming here however most people are creating a platformer game where as I'm trying to aim for a top down 2d, all directional character movement instead of the linear x axis character movement., and outside libraries.
I deeply apologise for my inability to find a suitable source. I have literally just came across an article online that came across a simple solution.
here:https://answers.unity.com/questions/221601/slow-animation-response.html
basically if you can't be bothered to click the link and you are having the same problem,
find exit-time by clicking the transition and then in the inspector and untick it.
Sorry.
I am using Unreal Engine to create a puzzle game. In this game, the player has to pick up a chair/table and place it on a pressure plate in another part of the room in order to unlock a door.
When play testing, picking up objects causes my character to slide back. Once the sliding starts, there is no way to make the character stop sliding. I suspect that this issue may be caused by the object's collision volume entering the character's. However, when I release the object, my character keeps gliding backwards even though by then, the object's collision volume is out of reach.
On my character's blueprint I have locked rotation on all axes, so that it won't randomly start rolling backwards after picking up an object. This solves part of the issue but there is still the gliding that just won't stop.
I am new to UE and I have no idea what may be causing this issue. If you have any previous experience in this or simply want to chime in, please do!
Another alternative fix is to lock the rotation of the collision component in the default pawn that you have or to increase angular damping.
To do so, open the default pawn blueprint and under the physics section, then constraints, lock rotation in the X, Y, and Z axis. Or increase angular damping.
Dont know why this is happening exactly unless we see the actual settings you are using and the BP but when you pick up the object , disable its physics and collision and when you put it back enable it after a delay of 0.5 or 1 second . Hope this helps.
I have a unity model with an animator attached that triggers idle to walk state.
The model is on a high ledge of a unity generated terrain.
I would like the model to fall off the ledge in a realistic way, i.e. bounce a bit/rotate coming to rest at the bottom.
If I add a character controller to the model it falls down but stays upright, not what I wanted here.
I tried using an empty game object that I added a rigid body and collider to and then added the model (without these things) as a child of the empty object.
I thought this had cracked it because the model rotated after leaving the ledge, bouced a bit on the way down but when landing at the base of the cliff went a bit mental rather than stopping at the bottom. Have run it a number of times and the model always continues to bounce around randomnly as though it is made of some weird magical rubber, sometimes it shoots off in the air again.
Where I want to go with this is that I have a first person shooter at the bottom of the cliff with a crossbow. The idea was that he shoots this guy at the top that then falls realistically to the bottom like a real person would.
Any ideas gratefully received.
I have my "player" gameObject standing atop a platform. He press a combination of keys, I verify that he's standing on the right kind of platform, and I run this code:
Physics2D.IgnoreLayerCollision(playerLayer, plataformLayer);
The intention being that he'd drop down from the platform. Instead, nothing happen immediately. The player is still atop the platform and can move around - only if he gets out of it and tries to get back to it, only at that point will he fall down from it.
So I made a temporary ugly workaround:
Physics2D.IgnoreLayerCollision(playerLayer, plataformLayer);
rigidbody2D.AddForce(transform.up * jump / 1.5f);
This will force the player to jump, making Unity "recalculate" the collisions and the player will, as I want to, pass through the platform. The even weirder part is: if the jump isn't high enough (100 force at 1 mass with 0.5 gravity scale seems to be the minimum), the player will still land on the platform, even though Unity is supposed to be ignoring those collisions.
I also tried pushing the player downwards, both with force and direct velocity, but no luck - he still collides with the platform, he only stops colliding with it after being away from it once.
Also, my player Rigidbody2D Detection Mode is set to Continuous, and I tried setting the platform up in many different ways, with and without a Rigidbody2D.
Any ideas on how to make the player instantly fall down from the platform as soon as collisions start being ignored? Thanks in advance.
I had been having the exact same issue. I have an alternative solution that you may find to your liking.
Rather than disabling or moving the terrain to be ignored, temporarily set the rigidbody2D of your player gameObject to kinematic. This will not cause you to 'drop' through the platform as you would like - but it will allow you to create the illusion that you are, by briefly using transform.Translate (or Vector3.Lerp, if you like) to move your character down via code.
This will disable Rigidbody2D.AddForce, but will still allow for set movement via rigibdody2D.velocity, if that is preferable to manipulating the transform directly.
Ensure the downward velocity lasts only as long as you are Kinematic, and try to let it match the gravity of your game, and it looks pretty seamless.
If you turn the rigidbody back on too quickly, it can (even with ignoreLayers :/ ) still jump back up to the platform - but if you leave it on too long, you can drop through more than one intended one-way platform, or even :O through the bottom of the stage!
This is easily avoided if you can be careful - personally I used both MonoBehaviour.Invoke to set a very short time limit on how long the player would remain Kinematic (about 0.5 seconds), AND I disabled Kinematic as soon as they moved 0.5 units from the point where the 'hopDown' was initiated.
If your gravity / platforms / character scale are vastly different than my own, you may have to experiment with some different numbers.
This has been my first attempt at an answer on stackoverflow, so I hope it has been helpful!
One easy way to do this is after calling
Physics2D.IgnoreLayerCollision(playerLayer, plataformLayer);
You reset the player's layer:
player.gameObject.layer = playerLayer;
It's sounds like you have covered a ton of possible solutions to this problem before coming here so, good job there. I listed a few alternative ideas below. I hope they help. Good luck!
Remove the collision object from the platform itself.
Offset the collision object to be in some remote location so it will not interact with the player. Offsetting the object may cause the collision to have a similar reaction to the jump you mentioned.