I am working on Sims-like building system.
I am at the point where I can create walls at runtime, but my problem is that I need to know when a list of adjacent walls create a well-enclosed room.
My code so far is pretty messy, so apologies if I don't provide any.
The setup is pretty simple, it is 3D environment where each wall is just a cube (see image below).
The game should allow players to build their own rooms made by walls prefabs (again, they are just cubes stiled as walls).
Any idea how to achieve this?
Related
I've been slowly working myself into Unity2D as i'm branching off from web development. As a result I'm currently stumbling against a rendering issue I've had where in a tilemap i have trees (and multiple other objects in the future with the same style) where one tree is made up of 2x2 tiles. The player needs to walk in front of and behind the tree based off of it's Y value. This works, only is the player counting the tree as 4 separate sprites so it displays the player behind one sprite at a time.
My question is, what would the solution be to properly render the player in front of the tree and behind. The solution needs to be dynamic as to where i can for example, make a player 3 tiles high and still work as intended.
Unfortunately i cannot place images on SO just yet as I've just made this account.
I have messed around already with the Project graphics settings to where I've changed the transparency sort mode and axis, the axis would be at 0/1/-1 now.
I've placed the player's layer and the tilemap's on the same layers.
The Tilemap renderer's mode is individual, according to online guides and possible solutions.
I hope the solution will bring me to a way where i can dynamically add anything like a crate, a barrel of 2 tiles high and anything in that sense where i just have to place them, i wouldn't have a problem if it'd be possible to create one tile which would be 2 tile spaces big.
This link might help you out: https://gamedev.stackexchange.com/questions/119734/unity-order-in-z-layer-for-objects
In this proposed solution however, the tree can't be part of the tilemap.
I downloaded a maze sprite for expiremental purpose to use in my game and I'm trying to add colliders to the wall. But since the maze is complex, it is quite a lot of work to add a Box collider 2D to each wall.
I tried using the Polygon collider 2D and it was some sort of inaccurate mesh looking collider.Is there any better way to add colliders to a maze or is it possible to do it programmatically adding colliders by somehow detecting the structure of the maze?
Here is something similar to the maze I'm using:
Add comment
There are a few answers to this question, depending on your approach.
1. Sprite-based Approach (where you have an image of a maze)
Make sure that the sprite is transparent, and only opaque on the wall areas (a PNG image can have transparency). After you do this, then you should be able to attach a polygon collider to automatically create a collider. If the image is too big/complex like you say, then you might want to split it up into several different images (4 quadrants for example), and then arrange them and attach a polygon collider to each object. In general, the simpler a collider is, the more accurate and efficient it is.
The downside to the above approach is that you are having to do a lot of manual work. If you knew that you had a lot of hand-drawn mazes that you would need to build colliders for like this, then it might be worth automating the process described above with a script, but that could get complicated fast unless you know what you're doing. Essentially, the automation script could recursively split the sprite into quadrants, create corresponding GameObjects, and add PolygonColliders to each one.
Manually splitting the image in a photo-editing program or making an algorithm to generate the maze and colliders might be faster for you than automation, depending on how much you want to get into the code.
2. Algorithm-based Approach
Luckily, there are a lot of maze programming tutorials online. Most are for 3D mazes, but the logic is the same to make a 2D maze. If you're interested in this option, then I found tutorials on the topic here, and here. In order to add collision after the maze is generated in each of these tutorials you can add a BoxCollider2D to each side of each cell which has a wall (or if using a prefab, add a BoxCollider2D to the prefab).
I'm new to Unity and I'm making a car racing Game. Now, I'm stuck at some point. I was looking for some solution of my problem, but couldn't succeed.
My problem is:
When I run my game on my phone, it sticks badly because whenever there are several buildings in front of the car camera, like one building behind another building, it lags. Reason for this is there are so many vertices and edges at that time, So the Car Camera is unable to capture all that stuff at same time.
How do I preload the 2nd Scene while loading 1st Scene?
I am using Unity free version.
In graphics programming, there is a common routine to simply don't draw objects that aren't in the field of view. I'm sure Unity can handle this. Check link: Unity description on this topic
I'm not hugely knowledgeable about Unity, but as a 3D modeller there's a bunch of things you can do to improve performance:
Create a simplified version of your buildings with fewer polygons for use when buildings are a long way away. A skyscraper, for example, can be as simple as a textured box.
If you've done that already, reduce the distance at which the simpler imposters are substituted for the complex versions.
Reduce the number of polygons by other means. A good example is if you've got a window ledge sticking out of the side of a building, don't try and make it an extension of the body. Instead, make it a separate box, delete the facet that won't be seen, and move it to intersect with the rest of the building.
Another good trick is to use bump maps or normal maps to approximate smaller features, rather than trying to model everything.
Opaqueness. Try not to have transparent windows in your buildings. It's computationally cheaper to make them just reflect the skybox or a suitably blurred reflection imposter. Also make sure that the material's shader is in Opaque mode, if it supports this.
You might also benefit a little from checking the 'Static' box on the game object, assuming that buildings aren't able to be moved (i.e. by smashing through them in a bulldozer).
Collision detection can also be a big drain. Be sure to use the simplest possible detection mesh you can - either a box, cylinder, sphere or a combination.
I have implemented a full per pixel collision system that accounts for rotation, and its very accurate. It returns a simple bool on collision.
However I am not sure how to handle the collision from the player movement point of view.
E.g. In the picture above, if the player is holding up on the left stick, he should be stopped, but if he is holding up + right diagonal on the left stick, he should slide northeast alongside the side of the red square almost naturally.
How do I go about this, to make the player’s momentum stop, but still give control for the player to move in direction not blocked by a collision.
I could do this kind of thing with simple untransformed rectangles, but going into per-pixel texture collision has made my brain explode today so I’m hoping you guys can help. Any advice would be massively appreciated.
It is possible. For that you need to implement physics engine with your pixel based collision if you are willing to give natural effect in your game.
For that either you write your own game engine or use engine that is already there. For 2D game I highly recommended Farseer Physics Engine. It is out there for a long time. Now in stable condition and it surely wonderful.
It is developed using XNA only from scratch, and also performance wise it is far good.
Have a look at this. I hope I was able to give you answer. Please let me know if more details needed.
I am going to make a game like XNA example game "Platformer1" which comes with the XNA. But I need longer levels which doesn't fit in the screen (like Super Mario levels). How can I manage this kind of level? Do I need to use a 2d camera that follows the sprite? If I do this way how can I load the level? I am a bit confused and I am not sure if I could explain my problem clearly. Hope someone can help?
The tutorial based on Platformer Starter Kit in MSDN has a step Adding a Scrolling Level which guides you through creation of longer levels. The tutorial is very detailed, I highly recommend it.
I couldn't find the tutorial in the section for XNA Game Studio 4.0, but differences should be minimal. According to the comment at the bottom of the page, all you need to change is replace
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None, cameraTransform);
with
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, cameraTransform);
in the tutorial code.
If you want to create a side scrolling game, then I would look into parallax scrolling. A quick google/bing will help you find lots of tutorials. Also, another useful tip is to search YouTube for XNA videos has we a lot of posters share their source code .
Here is a link to Microsofts Parallax Scrolling.
Sounds like you have a few problems ahead of you.
But I need longer levels which doest'n fit in the screen(like super mario levels). How can I manage this kind of levels.
There are several ways to do this, but a fairly easy way would be to have a 2d array (or sparse array, depending on how large your levels are) of a class named Tile that stores info about the tile image, animation, ...whatever.
Yes, you'll probably want a "camera". This can be as simple as only drawing a certain range of that array or a more featured camera that uses transforms to zoom out and translate across your level.
Hopefully this will help get you started.
I've done a decent amount of work in XNA, and from my experience, there are 2 ways to draw a 2D scene:
1) Strictly 2D. This method is much easier, but has a few limitations. There is no "camera" per se, what you do is move everything underneath the fixed 2D "camera". I say "camera" in quotes because the camera is fixed (as far as I know). The upside is that it's easy, the downside is that you can't easily zoom in or out or do other camera effects.
2) 2D in 3D. Set up a 3D world with a 2D plane. This is more flexible, but is also more challenging to work with because you will need to set up a 3D world and 3D camera. If this is your first attempt with making a game, I would highly recommend against this method.
I'm really only familiar with the strictly 2D method, and you would want a list of map objects that have a 2D coordinate. You would also want to store which section of the map you are looking at, I do this with a Rectangle or Vector2 that stores this. This value would move forward as the character moves. You can then take your 2D map objects' coordinate and subtract the (X,Y) of the top-left of what you are looking at to determine an object's screen position. So:
float screenX = myMapObject.X - focusPoint.X;
float screenY = myMapObject.Y - focusPoint.Y;
An other thing to note, use floats or Vector2/3 to store locations, you may not think it's required now, but it will be down the line.
It might be overkill, but my SF project uses XNA to draw a Strictly 2D scene that you can move around: http://sourceforge.net/projects/asteroidoutpost/
I hope this helps.
Have a look at Nick Gravelyns tutorials. They helped me tonne when I was first starting out - Really really worth a look for learning a lot on 2D games.
All the videos are now on youtube here