I'm making a 2D top down survival game, maximum sprite count for the top level is 100 sprites.
When I use a random to generate their vector positions occasionally i'll get some overlap between sprites.
So to combat this i'm going to store some pre-defined positions.
The question
So my question is what would be the best way of storing these. Initially i was going to store them in an array, however i'm thinking that storing them in a text file and reading them in at the start of the game would be a better solution.
I'm a beginner so if anyone could give any advice on this it would be much appreciated.
Many thanks
Yes store them in a CSV (comma separated values) text file, or if you want, use a database, although I would recommend the former. Database storage sounds like overkill in your situation. On start-up you would load the values into an array. If you don't the game will lag every time it gets a value. You just need the text file for persistent storage and then the array for usage.
Hope this clears your issue up for you!
Why don't you just check if the position of the sprites overlap? If the sprites do not overlap often this should not add too much calculations and grands more randomness then a fixed position template.
What you do in the class for the entities with the sprite is add a public Rectangle, you probably already use a rectangle to draw them to the screen. Making this public allows you to do something like this in the class that generates the entities.
if (addSprite)
{
Entity newEntity = new Entity(random position);
foreach (Entity e in entityList)
{
if (newEntity.rectangle(Intersects(newEntity.rectangle))
{
//give a new position to newEntity and run this loop again
}
else
{
entityList.add(newEntity);
}
}
Related
So I'm trying to create a VR maze game in unity with a fixed map using 1x1x1 default cubes and a c# script to store the map data. I'm a bit new to this kind of stuff so before moving on I wanted to ask more experienced people what kind of method I should use to store the data for the maze structure. I'm currently looking at a 100x100x100 maze, and my idea was to use 2d arrays with a 'y' or 'n' in each index that determines if that location has a block or not, and if it does, it takes the index as vector values and generates a gameobject at that location. I feel like there are definitely better methods that games like Minecraft use, but I have no idea how they work. Any help would be appreciated. Thanks!
In Minecraft, not all map data is stored. the only thing stored is one chunk, which is 16 * 16 * (map height) = 65536 blocks, and the seed value, and the blocks that user has modified. Using this seed value, map is generated as needed.
But in your case, it doesn't seem to be that there is some kind of map generation feature. So I think it would be enough to store tuples, like List<(int x, int y, int z)> If the block that contains wall is more than blank spaces, you can store the coordinates of the wall blocks, like (0, 0, 0), (0, 0, 1), ... If the blank space is more, then store the coordinates of blank spaces.
With regard to saving this list, you may consider the way of JSON Seriallization, or if you want to store just a list of tuples, you may construct your own way to store and parse it via .txt files.
Hope this answer would help :)
I am a beginner in Unity and I'm trying to make something pretty simple. However, I don't know where to start. I have a number of 'knots' in the scene, that the player will be able to select. They will then be able to connect two knots. I managed to find all knots in a range of 40 from the player's position and put them in an array. The next step would be to 'navigate' through the knots. I want the player to use the left analogue stick for that, but I don't know how to go about it. When they hold the stick at 120°, the knot closest to that position would be able to be selected by pressing another button. I looked at some UI navigation scripts, but this situation is pretty different as it's a two dimensional space. Does anyone have any suggestion on how to go about this? Thank you.
screenshot
It sounds to me like you're going to have to do some trigonometry to be able to find the nearest "knot." Each of these--at least if you're programming well--should each be an object whose x and y location variables can be accessed from your script.
You could iterate through the list of collected objects and compare them to your current location and store the object in a temporary variable if it is a shorter distance than the last object in the array.
Here's some pseudocode that should help illustrate my point:
for item in array {
Object closestKnotDist = Null
Float currentKnotDist = math.sqrt((player.x-item.x)**2 + (player.y-item.y)**2)
if currentKnotDist < closestKnotDist {
closestKnotDist = currentKnotDist
}
}
Thus, closestKnotDist will be the closest object to the player. Manipulate it how you will.
Not sure if this answered your question, but feel free to add some more explanation if not!
I imported a generated 3d model of a MRT scan into Unity. Unfortunately the model is imported into a lot of slices which make up the whole model.
The hierachy is like this:
As you can imagine it takes a lot of ressources and slows down further processes. So I want to combine the children (default_MeshPart0, default_MeshPart1, ...) of "default" into one mesh. I found out about Mesh Combine Wizard which works perfectly for objects with only a few children but it doesn't work with a larger number of meshes as children. I tried to move some children into another parent folder and combine them seperatly to combine the new parent folders later on but this doesn't work either as I got a random looking mesh.
Do you have an idea how to fix this or do you recommend me another process?
Perhaps you need to do some post-scan process.
Try:
Opening the scan model in a 3d software like Maya, 3D Max or Blender.
Attach or Merge all the parts together.
Export it as .fbx.
Import it into Unity.
In Unity will be a single gameObject.
Combining the meshes is actually pretty straightforward (I'll run you through in a minute), there is a gotcha however - while its easy to do in runtime, I've had some problems trying to reliably save the resulting mesh as an hard file asset to disk. I dit get there in the end but it was trickier than it seemed. You don't need to save your asset if you are okay in this being done in Start(), and you can do it in edit mode, but the resulting mesh will not be automatically saved with the scene - just something to have in mind.
Back to geometry - geometry consists of three arrays
Vector3[] verts;
Vector2[] uv;
int[] triangles;
The only thing to wrap your head around is that if you have n vertexes, uv[n] and vector3[n] describes n-th vertex (obviously), the triangles array is 3*n, and contains indexes refering to the vertex table, it goes in sequence, so if you want to find triangle k you read
x=triangles[k*3]
y=triangles[k*3+1]
z=triangles[k*3+2]
and than you have your triangle defined by three vectors verts[x],verts[y],verts[z].
There are also submeshes, used when a mesh contains multiple materials, but its not complicated once you know how the above structure is laid out.
Co create a new mesh, firs to GetComponentsInChildren, than grab a list of all the meshes, count the total vertex count j.
Create a new Mesh object, initialise it with tables verts = new int[j], tris = new int[j*3] and than just copy all the vertex data. Once you have the mesh, create a new object with a mesh filter and mesh renderer, feed the mesh with it and you're done. Problem is this will dissapear on play mode state unless you save the asset but that's a seperate question
if you have submeshes you need to list all the materials that are present and group vertieces by material used (sub group = a grup using a single material), aside from managing another list this isn't much harder (and nothing terrible happens if you ignore that step, you'll just get a single solid object) which is great for performance in most cases as your object will be a single drawcall not depending on dynamic batching)
It shouldn't be problem if its created for Unity3D. Anyway If you want single mesh, you will need a 3D modeling tool to combine it then export in FBX format according to unity guideline as explained here. I hope it helps you.
I'm building a Unity game where you have feelers, and by clicking the color given at that moment, you can get or lose a score, time or you get a game over screen.
My problem is - I have a lot gameObjects and each one calculates scores alone, (eache one have his own score int) and I need one code detects all scores number one, so I can see the scores one point (about how to see it, this is less important).
The name of the number that I want to find is called (how creative): score.
And yes, this is a public float.
I am most glad if I could write this document duration (unless you have no choice anyway ...)
sorry about any grammar mistakes, I do my best.
Thanks from advance
Roei Meiri.
:)
I can't image this would be very efficient, you would probably get better performance if you keep you own list of all the scoring objects(add to the list when creating the scoring object).
That being said...(uncompiled example).
Assuming you want to find of all objects with a particular component, like
class ScoringObject:Monobehavior
then you can find them all and add up the scores like so...
float totalscore=0;
ScoringObject[] listOfScores= Object.FindObjectsOfType(typeof(ScoringObject)) as ScoringObject[];
foreach (ScoringObject scorObject in listOfScores) {
totalscore+=listOfScores.score;}
http://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
Heey, i've been looking alot on Lidgren, and i've managed to get some simple console client and servers, but i'm having a really hard time with 2D...
Basically what i have so far is only Console based applications but i found an Example of a 2D game using Lidgren. You opened a server, and then two applications. They connected automatically and you could play with the two windows, seeing the character move in the other screen. That project used an array to load the textures and in the Draw() method it simple draws the array, but using a value from:
foreach (var kvp in positions)
{
// use player unique identifier to choose an image
int num = Math.Abs((int)kvp.Key) % textures.Length;`
// draw player
spriteBatch.Draw(textures[num], kvp.Value, Color.White);
}
Could someone explain what that num variable does? And if i wanted to use diffrent classes to do this, would i simply just do the same but in the player classes, and also, animations - how do you send texture update data?? Sorry that i'm asking so much..but i haven't found anything that actually helps :/
Thanks in advance and if you need to know something else, tell me! :)
The num variable simply chooses a unique texture, that will always be the same, from the textures array. Say you have 4 textures, but the Key the modulo operator (%) will get the remainder of 7/4, which would be 3. Basically it would wrap it around the amount of textures, because they would have to repeat (Ex, Id 4 and Id 8 have the same textures)
For now you can just use a static texture, but you can use the same code to choose a texture variation.
What do you mean texture update data? Such as changing textures, or updating positions? If changing textures, is the texture known or not? (Ex, can an ID/name be sent, or does the data have to?)