unity2d loading large worlds - c#

I am using the unity engine to make a "open world" (I'm using this loosely because its mostly the same) MMO top-down shooter. I have made the world, I have a really fast system to only load the part of the world you are standing on, But It still takes 5 or 6 minutes for it to load from the play button. basically what i have done is have a massive grid of tiles split up into 8x8 sections, and those into 12x12 regions. I would like a way to load the world as you go (like from a bitmap image or something), but would I also want to be able to edit and change the world in the editor. Anybody who has done this before can you help me figure out what to do?

Just off of the top of my head, I haven't used this in a released product but you can split your large scene into multiple scenes, you said you 8x8 tiles by 12x12 regions.
I would suggest making a base scene, that loads where the player is in the world, based off of his position you load a scene and a few scenes around him, as he navigates the world or gets to a place you determine would be in range for him to see other items you load the next scene. This is what it would look like:
OOOOOOOOOOOO
OOOOOOOOOOOO
OOOOOOOOOOOO
OOOOOOOOOOOO
OOOOOOOOOOOO
OOOOOOXXOOOO
OOOOOXXXOOOO
OOOOOOXOOOOO
OOOOOOOOOOOO
OOOOOOOOOOOO
OOOOOOOOOOOO
OOOOOOOOOOOO
each O represents a scene, and the X's are loaded scene. How do you load multiple scenes at the same time? If you take a look at the overloaded methods for SceneManager.LoadScene there is a method that takes in a LoadSceneMode Passing LoadSceneMode.Additive Will load a new scene into your world. without unloading the previous scene. So depending on how intensive your game is, you could Just load the surrounding area's to the player and load them over time in the background. The other option is to hold which scenes are currently loaded.
Next issue, being able to edit and change the world in the editor. Inside of the Unity Editor you can have multiple scenes open at once, and move and relocate each item in those scenes. The scary thing about this though, is to add a new GameObject to a specific scene you have to have that scene set as the active scene.
Here is a link to the documentation on Multi-Scene editting:
https://docs.unity3d.com/Manual/MultiSceneEditing.html
Documentation on SceneManager.LoadScene:
https://docs.unity3d.com/2018.1/Documentation/ScriptReference/SceneManagement.SceneManager.LoadScene.html
Documentation on LoadSceneMode.Additive:
https://docs.unity3d.com/2018.1/Documentation/ScriptReference/SceneManagement.LoadSceneMode.Additive.html

Related

Unity 2D OnTriggerEnter2D() will not repeat in an entirely different script

I am creating a game where there are 9 chunks in 1 2D map and you must "unlock" every chunk by triggering a collider on the edge in a specific order to beat the game. I have managed to switch the scene from 0 to 1 using this code on a rectangle with a trigger collider:
void OnTriggerStay2D(Collider2D other)
{
SceneManager.LoadScene(1);
Destroy(this.gameObject);
}
The code above works fine, but recently I created the third chunk and copied the code to a new script, changed the name, and switched SceneManager.LoadScene(1) to SceneManager.LoadScene(2), then added it to a new rectangle with a trigger collider. The second chunk loads in when triggered, but the third does not. Is there any way to fix this?
I don't have enough reputation yet to comment, so I'll propose a few possible problems/solutions. I assume your chunks are scenes, so you're loading in several scenes at once? Some things I can think of that might be causing the second chunk not to load in:
Did you add the scene to the build? If not, you can do so in the build settings.
You can try to debug OnTriggerStay2D() in the second chunk trigger. Is it being triggered? If not, you might not have added the trigger to the rectangle correctly: did you set up the Rigidbody2D?
If the above points do not fix your issue, I'd suggest adding more of your code (of the chunk loading scripts) and some screenshots (of the inspector in both chunk loaders and of the colliders you made) to your question. Hope this helps.

Screen flashes when loading new scene

I have a level selection menu, inside this I have buttons to load my scene levels.
I am using the next method to switch the scenes:
UnityEngine.SceneManagement.SceneManager.LoadScene();
The problem is that I have dark-gray faders in my scene and when I load the different scenes the screen flashes white. Instead of staying in the same dark-gray tone like I would want it.
I will gladly supply any further information that you need.
Does anyone know a solution?
The last frame of the previous scene is contiguous with the first frame of the next scene so one needs to ensure that there is nothing in either frame that could make a visible difference e.g. resetting the state of something like a material colour via a script called from an Awake function.

Changing Sprites using scripts in Unity2D C#

I am wanting to make a birds-eye view pixel-art game.
I currently have two sprite sheets set up, and split and whatnot
groundSheet and characterSheet these are split up into
ground_0_0_0 (A concrete floor)
ground_1_0_0 (grass)
character_0_0_0 (man idle animation frame 1)
character_0_0_1 (man idle animation frame 2)
character_0_1_0 (man run animation frame 1)
character_0_1_1 (man run animation frame 2)
character_1_0_0 (woman idle animation frame 1)
character_1_0_1 (woman idle animation frame 2)
character_1_1_0 (woman run animation frame 1)
character_1_1_1 (woman run animation frame 2)
The numbers after are a note as to:
first number - the main set of sprite animations (eg man)
second number - the animation set in use (eg run or idle)
third number - the frame of said animation.
(the ground has this as i plan to have animated grounds late on)
Now, I wish to make a script for the character (and ground alike) that has an editable value that is view able in the unity editor, for example how things like the sprite renderer has sprite, colour etc. Which dictates what first number to use (see above) what second number and the delay for the animation of the third number. This will then tell the sprite renderer what pictures to load and how quickly to load them. I also wish for the script to scan for the file starting with for example character_0_0_ and then count how many files after it, so it knows what to do when animating. I would like this script to be checking for a change in one of the variables viewable in the unity editor to change and as soon as it does it checks everything it needs for an animtion.
Something else could be done where there is only 1 box to edit in unity, which you put character_0_0_ or ground_1_0_ or something similar, and it checks everything that way (it also makes the script universal, and usable on the ground, character and walls (which I am adding later)).
This may seem confusing, but it make sense for me and many of you will probably mention a much easier way to do animations and such, but please only say these if it does what I want above.
For scripts and such my file layout:
/Assets
/scripts
ground.cs
character.cs
/sprites
characterSheet.png
character_0_0_0
character_0_0_1
character_0_1_0
character_0_1_1
character_1_0_0
character_1_0_1
character_1_1_0
character_1_1_1
groundSheet.png
ground_0_0_0
ground_1_0_0
(For some reason Stack overflow said the above was code, so i had to make it as that)
ground.cs and character.cs are the scripts in which I want to made as explained above.
In my object view thingy I have
Main Camera
ground
character
I am practically a newb to C# and JS I know bascially the grammar of C# (like where to use {} and put ; at the end of the lines). If you help me with this, i request that you explain the script, like use the // thing to simply explain what each command does, I know a few but not all of them. And I know someone is going to say it is really well documented in tutorial X and such, but most tutorials are not in unity 5 and while helping with the matter do not touch on it exactly.
Thank you for your help, if there is anything about this question/request that you do not understand (It is pretty complex) I will explain.
Maybe I am completely wrong, but it seems to me that you are trying to recreate an Animation system.
Is there a specific reason for which you wouldn't use Unity's animation system?
You can do everything that you describe here with it (change sprite, choose animation speed). And you would have almost no code to write. Just drag and drop you sprites in the editor for a start
EDIT - answer to first comment:
What you should do is create all the animations you need. Then in the animator, you choose which condition will trigger a transition to another animation (for instance, boolean jump is true will transition to animation jump). Then in your code you can call GetComponent().SetBool("Jump", true).
To have different character, you can create different gameObjects (one per character). They all have a different animator with animations specific to the character.
The other solutiojn if you really want one one gameObject and one animator is that you also add string condition to you animation (example, if character=="character1" and jump==true, transition to jump animation).
If I were you I would really focus on testing and learning all you can do with Unity animator. I can't think of a case were you would need to recreate the animation system yourself
Your question was long winded and hard to understand but ill try to help,
firstly if you want editable values in the unity editor I would Suggest using a serialized structure of information like this
[System.Serializable] // this makes it viewable in the inspector
public struct Sprite_Manager;
{
public Sprite[] Render_Sprites; // array of sprites to store sprites
public SpriteRenderer S_Renderer;
public float Anim_Delay;
}
public class Character : MonoBehavior {
Sprite_Manager SMG = new Sprite_Manager(); // instantiate a new instance of the struct
void Set_Sprite()
{
for(int i = 0; i < SMG.Render_Sprites.Length; i++)
{
SMG.S_Renderer.sprite = SMG.Render_Sprites[i];
}
}
void Update
{
Invoke("Set_Sprite", SMG.Anim_Delay);
}
}
Not sure if this is exactly what your looking for but this is one way you could setup a structure of sprite based information and use Invoke to setup some sort of delay when passing new sprites to the renderer.

Unity - How do I apply an animation from one game object to another rigged game object?

I am thinking of building a game like the sims and was thinking of how I could make the characters interact with any object without having to systematically change the character's code and prefab whenever I decide to add new furniture and props to the library. I have had an idea that consists of having my animations attached to the furniture itself rather than having it attached to the character (which would require a lot of memory and extra code if we had a lot of furniture in the game library!). I was thinking about it and had in mind that I could add a rig skeleton in Blender that has the desired animation(s) but not rig any specific object - so that I only have the animation, the location, rotation and scale of the bones! and since the characters' rigs are the same as the skeleton's rig in the furniture's animation, they could be compatible!
I hope this makes sense...
The thing is that I realise at this stage that I do not know the Animation class very well, and I am not sure if / how I can use the animation attached to the furniture to make my character interact with it...
Can Animation.Play() take multiple arguments apart from the animation to play and the play mode? Is it possible to specify which object has to play the animation? I am talking about doing all of this from the script - I write in C# by the way... and I might attach the script to the camera so I hope the script does not have to be necessarily attached to the character prefab in order to play the animation... Though I don't mind creating a function in the character's script if necessary!
I am not new to Unity but I have mostly been using cars and character controllers, so I haven't dealt with much animation coding yet...
I thank you in advance for your help and apologise for my weird english - I live in France! ^^
Mecanim performs retargeting of humanoid animations, so for what concern that specific case, you should be able to use the same rig and set of animation clip on different models.
For what concern grabbing furniture, you don't need the object to be rigged. You can use IK to precise move your character's hand toward the target object and then parent it to the relative joint in the hierarchy.

How best create a game map in silverlight

I'm looking for some idea's on how to create the 2d layout (walk path) of a game.
The tiles will be 32x32 pixels aswell as the character moving on the tiles.
I've thought of two ways currently, havn't tried any of them out yet.
The game will be shown in a window of about 400x300 or 512x356 something.. not much bigger than that where the game map itself could be bigger then 1000x1000. So only a portion of the map will be visible most times.
So here's my 2 options as far as I've thought of them.
[1]: Create the entire map at the start (which might increase the loading times but it will make it more easy to move the map around) including each object, player, enemies etc etc.
[2]: Create only the map seen and 1 tile around it (which is not visible) so I can make that one visible with a story board and remove the row / column which is out of bounds and create a column again.
Which means, If I have a room of 5x5 tiles, and I load 7x7 tiles. If I move right, I move all the tiles left and keep the player in the middle of the screen. Then I remove the far left set of tiles and create a new set of tiles on the right side, which will hopefully be done before the storyboard has finished moving.
In the second method all I have to consider is objects that are larger then 32x32. How could I best react on those.. However, this is a problem I'd like to address later.. Right now I prefer to know people's opinions and possible better methods.
I do apoligize if this question is no apropriate for stackoverflow.
Just be careful with the "load" word. Loading, means taking the data from the disk/drive and putting it memory. For that, you should load all the current world/map at once, before the game start. Trust me, you don't want to get yourself in dynamic loading stuff while the map is running.
But you only need to load the definition of your map, you don't need to create the instance of the object at that point. So you can create the instance from the map definition as the player walks... but are you really that constraint in memory? I can understand that for a game like Minecraft or Terraria where the map contains virtually millions of tiles... But for 32x32, you can consider making them all when you load the definition.
Of course, whatever the solution, you should only draw/compute/collide/update the tiles that are visible on screen.

Categories

Resources