Unity Game View Line - c#

I'm learning Unity but when I create a map with Tile Map and look at the map in the game view it has some lines

It's tilemap aliasing, usually goes away when you try a standard resolution in the gameview, which you already have but try 1x scale, also try something like 720p, 1080p or an actual monitor resolution and make sure the game view is set to "Fixed resolution". If the aliasing disappears, you will know it only appears at weird resolutions and you won't have to worry about it.
If it doesn't, theses are some of the things you can try:
First make sure that the filtering mode for your tilemap sprite/s is set to point.
Add a new material to the tilemap gameobject and set it's shader to Sprites/Default, then check the "pixel snap" box.
Try the pixel perfect camera https://blog.unity.com/technology/pixel-perfect-2d
Set the grid cell gap on x and y axis to a very small negative number, like -0.0001
If all of these doesn't work, I think you will have to use the sprite packer to pack your tilemap into a sprite atlas and make it so that there are gaps between each tile. If you wanna know why that is: https://forum.unity.com/threads/tilemap-has-tearing-between-tiles-even-with-pixel-snap.499154/#post-3292216

Related

How can i repeat a texture at its original scale on my cube in unity

So as you see in the picture, i made a texture repeat on a rectangle(its size is 40,10,60) but it repeat the same amount of time on every face,so depending of the size of the face the texture is stretched.
In the picture you see that on the top face the texture repeat correctly and keep its original size but on the other faces it is streched.
Is there a way to repeat the texture without changing its size ?
Thank you for your responses.
screen of the problem
Edit : this script in c# does exactly what i want but is there a way to do it without a script since it was done in 2017 ?
https://github.com/Dsphar/Cube_Texture_Auto_Repeat_Unity/blob/master/ReCalcCubeTexture.cs
Unfortunately you will probably need to create a new material with the same image for each different scale using the 'Tiling' attribute:
*** Edit #1
The x and y Tiling values need to proportional to the scale of the plane or it will stretch.
If the size of the mesh being textured is static, you can change its UVs in a 3d program. You could even change the UVs via script.
Another option would be to look into worldspace (triplanar) shaders. These texture based on world position rather than the vertices local position.
If you are using Shader Graph, look at the triplanar node.
https://docs.unity3d.com/Packages/com.unity.shadergraph#6.9/manual/Triplanar-Node.html

Why is Unity Canvas Image always rendering over my gameObjects even though it is definitely behind them?

In my 2d Unity project, I have a Canvas with an Image that I want for a Background.
I have 2 gameObjects in front of this background. But no matter how much fiddling I do with Pos Z, Sorting Layers, or hierarchy sorting, the image is always in front of the objects.
Gif above shows in 3d mode that even though the image is clearly behind these objects, it will always appear over them if they overlap.
Hierarchy:
Main
Camera (Inspector: https://i.imgur.com/Q5a52cf.png)
BackgroundCanvas (Inspector: https://i.imgur.com/m9Pxr6B.png)
BackgroundImage (Inspector: https://i.imgur.com/jTx7pEW.png)
Object1 (Inspector: https://i.imgur.com/YcClEhk.png)
Object2
Any advice to rescue me from this madness is much appreciated.
Set the sprite renderer's transform z value to 0 instead of 100
If that does not solve, please specify camera properties also, so I can try to recreate the exact setup.
Try clicking on Layers -> edit layers, inside sorting layers you can change the order grabbing layer, everything upper appears behind in the camera.
You could create a layer called Object
Assign it to the game objects.
Create an object camera
culling mask -> object layer
depth bigger than you current main camera.
Set it to Projection -> Orthographic
Clear flags -> solid colors.
canvas Render Mode -> Screen Space - Camera and assign the Render Camera to be the Object Camera
Inspector tab of the object or background.
Sprite Renderer.
Additional Settings.
Sorting Layer.
change it to a different layer.
Had this same issue and was able to fix it with these steps:
In canvas settings change Screen Space Overlay to Screen Space Camera
Set the camera variable to the one you are using for your scene.
I figured out a workaround. I basically created a VisualElement inside the UI Builder and set a render texture to the background. Then I added an extra camera to my project to view all the sprites that needed to be on top. That camera feeds the render texture, so now everything that camera sees is forced to be on top of the UI Document as the background of that VisualElement. If you want control over the whole screen, just set the VisualElement position to absolute and max out its dimensions. If your game doesn't have a fixed aspect ratio it might cause some stretching, but other than that I cant really tell the difference. Sorting layers for the UI Documents are broken and unity needs to work on that. This is the best option I've found. Hope this helps.
I had the same problem and I fixed it by attaching the camera to the canvas which is screen space and finally changing the sorting layer of my object to -1.

Unity TileMap Grid problem with creating a simple fog of war

I have a procedurally generated dungeon BSP on a Tilemap Grid. a TileMap collider has been added to it.
I planned to do it simply, since my TileMap contain a SpriteRenderer, I add the color 0,0,0 to it like a black fog of war, and if I can't see it well or have already opened it i would repaint them with 0.5,0.5,0.5, and the area around the player would make 1, 1, 1. and that would be cool, BUT I only can get tiles from the map via VectorInt, or via an index and a copy of the array. Can I access each cell, somehow through a collider? To use something like Raycast or Overlap Colliders.
any collider or collision tells me about the TileMap map. Or do I still need to create each tile and attach them to a game object or Scriptable object? I'm confused, tell me, I reviewed many tutorials and questions on this issue and got confused
I don't completetely understand, but I'm pretty sure you want to add a tile during runtime? Or you at least want to change the tile so that it's black or has a fog of war?
First off, I found this link: How can i place a tile in a tilemap by script
However, that would be difficult, having to have some kind of array, or possibly 2d array, of all the tiles for when there shouldn't be a fog of war.
My suggestion is using a Sprite Mask , and setting the tilemap(the actual ground) to not interact with the mask, and having a big fog of war sprite, like a big black cloud that might follow the player around so that it doesn't need to be a massive sprite, and set it's sprite interaction to visible outside mask.
Finally, make a sprite mask that follows the player around, and therefore will remove the fog of war around the player.
Another thing is to make a big cloud sprite with a hole in the middle, and have it follow the player around. That way, there's a big fog of war, but there's a hole in the middle so the fog is not around the player.

Why does Unity's Legacy Vertex Lit disable my code that sets the color of tiles on a 2D tile map?

To start, I have a bird eye view 2d tilemap that I generate procedural. I color each tile using TileMap.SetColor( new Color(r,g,b,a));. This works very well with the default shader that does not process light on the scene. I want light in my game, so I swapped the shader, 1 by 1, to every other shader with none supporting the custom color and lighting at the same time in Legacy vertex Lit Mode. I tried swapping my rendering mode to Forward and Deffered and this did work as I had my custom colors on top of the lighting, but problems occurred. The reason I swapped to Vertex Lit in the first place was because I was getting artifacts at the boundaries of every tile with lines that showed the cross sections of tiles that overlapped and I get a way lower frame rate in forward/deffered, so I prefer legacy. I know why the shader does this and I tried writing my own, but I am very new to Unity, so I am not experience enough to dive into graphics that far.
What Is Needed: All that is needed is my custom tile colors and lighting in Legacy Vertex Lit mode or A fix to the overlapping tiles lighting up in forward and deffered modes.
Thanks again for the help! I am losing my mind trying to solve this.
EDIT: I noticed that when I changed the layer or z position of a tile in that map, the effect disappeared completely in forward and deffered of just that tile only. I don't know if this the solution I am looking for, but its a start.
EDIT #2: I set the chunk rendering mode to individual and it fixes the problem as in the first edit, but performance takes a very big hit. Any way to tell the render/shader that each tile is separated?
Ok, I eventually got it to work. What I did to get rid of the lines and get custom coloring in the game was to swap rendering modes to forward mode. Then, you can go into the shader you are using for the tilemap and add #pragma noforwardadd.

Unity2D Gameobjects covered by canvas

I am having a problem with my unity canvas as my background is on top and covering my gameobjects. I have tried putting the canvas sorting order to -10000 and changing the canvas rendering mode to overlay/camera/worldspace, but none of them works..
This is how my game looks like right now:
as you can see the white image which is my background is covering my gameobjects..
Attached are my component values for my canvas:
my canvas
Attached are the values for my camera:
my camera
Attached are the component values for my gameobjects:
my hero gameobject
Have you tried setting your game objects z position to a negative value?
For 2D games in Unity, the more negative your Z value is, the "closer" it is to the camera. If you try setting your knight to something like -1, it should no longer be obscured by your background.
Also, make sure to set the camera for your canvas' render camera and you can set the sorting order back to whatever it was before. Just check the individual pieces attached to your canvas to make sure their Z positions aren't "in front" of the game objects.
The background for your game shouldn't be on the canvas, it should be a gameobject with a sprite renderer and need to be attached with a sprite(background image) with a resolution that will fit most mobile screens (assuming you build it for mobile phone, i personally prefer making it 1028X720)
if you still insist on putting it on the canvas then you could set the canvas render mode to world space and set the sorting layer to something less than default or sort order -1
I know it is old post but #user8502296's answer saved me so I wanted to contribute. I changed the Canvas->RenderMode->Screen Space-Camera then I added Render Canvas->Camera->Main Camera.
Canvas

Categories

Resources