Does disabling graphics game objects prevent them from being affected by scripts? - unity3d

I'm using frames (bounding boxes) to disable all game objects within an area when the player is not in that area. I want to know if parallax scripts will affect a disabled frame of objects. I don't want the parallax of background and foreground layers to be off when the player enters a new frame.
Slightly off-topic, but still relevant enough: I'm building the frames to be seemless transitions; is there a way to prevent parallax overlap at the transition point?

Scripts on disabled gameobjects do not recieve Update calls, as you've noticed. There are a few things you can do:
Not disable the entire gameobject when its outside the camera frame, instead only disable the renderer attached. This way your parralax script is still updated. Though its questionable if there are performance benefits to this, Unity does Frustrum culling by default.
Dont have individual parallax scripts, instead have one ParallaxManager in which you register all objects. This manager is never disabled, as an added benefit there is less overhead calling update.
Instead of update start some coroutine on another object, that isnt disabled. For example, in Parallax you can start a coroutine from the Camera gameobject. That way it keeps going, eventhough the parallax is disabled.
Try to calculate the right position in the OnEnable. That way you can still disable objects, and they appear at the right position when they are enabled again.

Related

Animation changes transform when entering game mode

In my Unity 3D project I've added an animated body as a child to the Player object. The player moves as is should, with the right animations displaying for each key. The problem however is that the transform of the child changes when I enter game mode, meaning the character is walking above the ground. In scene mode it's relative to it's parent, it's grounded, but when I switch to game mode the character suddenly changes it's y-axis transform. I also checked the inspector for this object, it's y-axis changes.
I tried disabling the movement script, to check if this could have anything to do with it, but nothing changes. The transform still changes. I'm new to this problem, and every thread I've found on the topic hasn't helped much. Does anyone know what might cause this?
The hierarchy. mixamorig:Hips is the one changing it's transform. The animations and animation skin I'm using is from Mixamo.

Does changing multiple sprites on a single animation affect performance?

I have been building a 2D sprite based game for which I want to have the player be able to customize their equipment. This means that although I am fine with drawing the content, I'd need to ensure animations in the game run fine on top of each other. For this, I have been preparing a game object with several children to account for the equipment:
Each of the children runs a single animation and should have to follow the player, which I accomplish by using transform.localPosition = Vector2.zero; on the Update of the script each takes, so they hook to the parent's pivot and follow the player. While this has worked for the most part, there are moments in which all of the objects are not synchronized and as such sometimes the parent object (the body) is seen where it shouldn't since the other game objects should render on top:
Aside from that, to make it easy for the children to follow the parent position I had made sprites which are all the same size, which risks me having to load a lot of transparent space per sprite.
Another problem that I just noticed as I'm trying to address the issue of loading too many useless pixels involves the positioning with other objects such as the Sword game object, which doesn't follow the player fully if I use sprites that are not perfect squares (see this question for details How to align sprites of smaller sizes to a moving gameobject sprite? and this one Sibling sprite doesn't appear to follow main GameObject sprite even when transform.position updates)
I tried to fix this by making the Sword a child of the Hero, but even then changing the position through a function that sets values to add on to transform values of the sword game object only change the position of it relative to the initial value. I attempted changing the pivot of the sword sprites to a custom value to guess where the center of it would align with the main game object and appear in the right position, but even that doesn't seem to work.
I'm kind of getting tired with my current process, as I have to rely on several animations for each of the game objects, both parent and children, so that these obey to different layers in a single animator (or in the case of the sword, a separate animator), all to ensure there is some synchronization that doesn't always occur:
I really don't mind the web that is turning out in what I'm doing, but the fact that I have to repeat it across multiple layers with no real guarantee that all the objects would appear right on top of each other due to the fact of having multiple animations playing, and loading multiple sprites with empty space is becoming more of a chore than enjoyment.
So I think I came up with a possible solution: If I could make a single animation for the whole equipment used at any given point (whether only wearing pants or wearing full equipment), then having this single animation could guarantee synchronization across parent and children without the need for animator layers or special functions to update position or worrying about pivots or square sprites if I can set the position of non-square sprites in the animation, with the downside that I would need to account for every single animation for each possible equipment variation (so if I had even 3 of each sword, pants, boots, etc. that would mean 3^6 animations) and make a more complex web of animator states. The only thing I'd be worried about in this case, however, would be the performance, if having too many animations for a player would affect how fast these load. But at the benefit of eliminating the other problems mentioned, my question boils down to this:
Is it better to have a single game object with animations that change multiple sprites across children game objects and a single animator that chooses states based on multiple variables, or game objects with multiple animations that change a single sprite for each, and a single or multiple animators with multiple layers that choose states based on multiple variables?
There isn't really a set answer for something like this. It really just depends on how good your/the players computer is when playing the game. Sorry if this isn't what you wanted.

Are sprites automatically removed?

I'm currently trying to code some falling stars in my game, and they seem to disappear once they move out of the screen. But I'm not sure if swift is actually automatically deleting for me, or if they're still lingering around.
I'm starting them at the top of the screen and using physicsBody to use gravity to bring them down.
star.physicsBody = SKPhysicsBody(rectangleOf: star.size)
I know apple documentation says "Later, if the sprite is removed from the scene or is no longer visible, SpriteKit can delete the texture data if it needs that memory for other purposes."
https://developer.apple.com/documentation/spritekit/skspritenode/getting_started_with_sprite_nodes
but I find that kind of vague and just want some confirmation that because I can't see my star sprite anymore on my screen, I can also assume that it's being removed.
(and not just clumping up somewhere off screen)
No - it's not automatically removed.
By 'removed from scene' the SK documentation doesn't mean that if you can't see it anymore, it means that the 'RemoveFrom...' method was called on the sprite in question.
If the sprite isn't visible, then the game engine won't draw it and could optimise memory by removing it's texture, but the sprite is still being tracked and colliding and bouncing off objects etc.
The SK Scene extends infinitely in all directions and your device's screen is simply a moveable window onto the scene. If you decide in your game that a sprite which moves offscreen is no longer needed, then you need code to detect this and then issue 'RemoveFromParent' against the sprite.

Does setting camera's culling mask to Nothing when UI covers the screen is good?

I have a pretty large and full scene and therefore gets a lot of draw calls.
Sometimes I display a video in the game, which covers the entire screen.
When I tested my game with Unity's profiler tool I noticed that the camera still renders everything (although occlusion culling is enabled and calculated), and it causes the video to lag.
My question is how can I disable the camera?
When I disable the Camera component or the camera's GameObject I get a warning ⚠ in the game view that says No camera is rendering to this display. Which, I guess, is not good (correct me if I'm wrong).
So I was wondering if cancelling the culling mask on the camera (by setting it to Nothing) would force unity to stop render the scene.
Or does it still do some work in the background?
(Like with UI elements that still being rendered even though they are fully transparent).
Thanks in advance
I have a pretty large and full scene and therefore gets a lot of draw
calls.
I recommend activating "Instancing" on your materials, it can greatly reduce draw calls.
When the UI Pops open, it can help removing the "Default" layer (or whatever layer the majority of your renderers are) from the active cameras. You can do this easily with layer masks. Or you can just set Camera.main.farClippingPlane to 1 or any low number.

Unity - How to change player spritesheet while runtime?

I'm currently working on a 2D pixel Jump'n'Run. I want the player to be able to "buy" new skins for the player-character. I have multiple sprite-sheets. They all have the same structure. I'm using sprite animations.
How can I change the sprite-sheet at runtime? I found the following solution, but it's very resource intense: https://youtu.be/HM17mAmLd7k?t=1818
Sincerly,
Julian
The reason it's so resource intensive in the video is because the all the sprites are loaded in each LateUpdate(), which is once per frame. The script looks like it's grabbing all the sprites in the sprite-sheet and loading them every frame so that if the spriteSheetName ever changes, it will update the renderer on the next frame.
I don't believe that's necessary and in the video he mentions that it's just being used as an example. What I'd do is move it out of the LateUpdate() method and into its own method that can be called only whenever the user wants to change the sprite-sheet. So instead of mindlessly loading the sprites from the sprite-sheet each frame, you'll only load them whenever the user selects it.
That should accomplish drastically cutting down the intensity of this script because you're not loading all the sprites in a sprite-sheet and looping through each of their renderers on every single frame.