Unity - healthbar as outlined line around the screen - c#

I have a game in Unity.
The idea of the game is that an object is spawned on the screen.
It exists for several seconds.
You need to click on it, while it exists.
What I want to do, is that while it exists - I want to show a timer, but not with numbers, but with an outlined line on the perimeter of the screen.
I know, how to make a make a slider and how to make circular health bar, yet I do not really have an idea to make a slider, that is decresing from everywhere to the centre as you can see on the screenshots below.
I would be thankful for the inspiration!

This should be a very good fit using alpha cutoff with a custom shader.
Here's a video explaining it in detail (it's only the first half you will need).
The basic idea is to create a texture with a graded alpha depending on how you want the texture to appear/disappear. In this case the alpha value at the top and the bottom would be close to 0 and then gradually around the line on both sides towards the middle increase to 1. The shader then cuts off the texture below a cutoff threshold which you can change depending on the value of the timer.

As a dumb and simple approach:
Have two images with fill, one goes middle towards top the other middle towards bottom.
Just imagine the pnguin is your outline ^^

Related

How could you create a ultrasonic vision in Unity3d? (Bat Vision)

I had the idea to create a game where you would see everything like a bat. There are some people which have achieved something like that.
The problem is I would like to have another type of "Bat Vision". It is pretty hard to explain but if i look into one direction and then rotate the character I would still see something I looked at before.
Explanation with timestamps:
Sec 0: I am looking at point A where a object is 300 meters away
Sec 1: I rotate to point B and see an object 100 meters away
Sec 2: I see object A and B at the same time
Sec 3: I still see object B
Explanation with picture:
As you can see the player first shoots the ultrasonic waves a first on A and afterwards waves b on B. They both come back at the same time so that both objects are seen at the same time.
If you did not understand why I see objects delayed. As everyone knows sound travels with a speed about 1/3 km/s. So an object which is 300 meters in the distance would get seen after about 2 sec.
I already tried to shoot RayCasts or GameObjects into the direction and as further they are away the darker the pixel at that position will be. But as you can think... Shooting 2 million RayCasts every Frame is not very healthy. I was also thinking about Shaders but I don't know how it is possible that one still sees the Shaders while looking into another direction. Also I don't have much experience with Shaders and would need help there anyways.
I would be glad if someone has another Idea how I could create this.
To achieve this the easiest way would be using an image effect shader that accesses the depth buffer.
The value of each pixel in your depth buffer is by definition the distance of that pixel in the world to the camera
You can decrease the value of each pixel by some value every second and set the value to max every X seconds based on the distance given by the depth buffer... this will give the effect of sound bouncing off that object back to your player
https://www.youtube.com/watch?v=OKoNp2RqE9A
tweak the colors to achieve needed effect
It appears to me that maybe you want to duplicate any meshes and surfaces that your player has 'seen', and keep this in view with some grayish light settings. And the 'real' objects are all hidden, that is, they all have their MeshRenderer components disabled or removed.
If the player shall see an item like a shadow in front of him even though he moves, you could have the cloned visible duplicated move along with the player some time until they fade.
To find out what to duplicate I am sure you'd need a lot less raycasts than doing it for each pixel. Clone the mesh, then check visibility for each vertex and set the visibility in the duplicated vertex color and alpha.

Getting "giggly" effect when slowly moving a sprite

How do I remove this "giggly" effect when slowly moving a sprite?
I have tried adjusting Antialiasing values in QualitySettings and Filter Mode in ImportSettings in the Unity Editor but that doesn't change anything.
Ideally, I would like to keep the Filter Mode to Point (no filter) and anti aliasing turned on to 2x
The sprite is located inside a Sprite Renderer component of a GameObject.
I have uploaded my Unity Project here: http://www.filedropper.com/sprite
I really don't know how to fix the problem... Can anyone help with my personal project?
I cooked up a quick animation to demonstrate what's happening here:
The grid represents the output pixels of your display. I've overlaid on top of it the sliding sprite we want to sample, if we could render it with unlimited sub-pixel resolution.
The dots in the center of each grid cell represent their sampling point. Because we're using Nearest-Nieghbour/Point filtering, that's the only point in the texture they pay attention to. When the edge of a new colour crosses that sampling point, the whole pixel changes colour at once.
The trouble arises when the source texel grid doesn't line up with our output pixels. In the example above, the sprite is 16x16 texels, but I've scaled it to occupy 17x17 pixels on the display. That means, somewhere in every frame, some texels must get repeated. Where this happens changes as we move the sprite around.
Because each texel is rendered slightly larger than a pixel, there's a moment where it completely bridges the sampling points of two adjacent pixels. Both sampling points land within the same enlarged texel, so both pixels see that texel as the nearest one to sample from, and the texel gets output to the screen in two places.
In this case, since there's only a 1/16th scale difference, each texel is only in this weird situation for a frame or two, then it shifts to its neighbour, creating a ripple of doubled pixels that appears to slide across the image.
(One could view this as a type of moiré pattern resulting from the interaction of the texel grid and the sampling grid when they're dissimilar)
The fix is to ensure that you scale your pixel art so each texel is displayed at the size of an integer multiple of pixels.
Either 1:1
Or 2:1, 3:1...
Using a higher multiple lets the sprite move in increments shorter than its own texel size, without localized stretching that impacts the intended appearance of the art.
So: pay close attention to the resolution of your output and the scaling applied to your assets, to ensure you keep an integer multiple relationship between them. The blog post that CAD97 links has practical steps you can take to achieve this.
Edit: To demonstrate this in the Unity project you've uploaded, I modified the camera settings to match your pixels to units setting, and laid out the following test. The Mario at the top has a slightly non-integer texel-to-pixel ratio (1.01:1), while the Mario at the bottom has 1:1. You can see only the top Mario exhibits rippling artifacts:
You might be interested in this blog post about making "pixel-perfect" 2D games in Unity.
Some relevant excerpts:
If you start your pixel game with all the default settings in Unity, it will look terrible!
The secret to making your pixelated game look nice is to ensure that your sprite is rendered on a nice pixel boundary. In other words, ensure that each pixel of your sprite is rendered on one screen pixel.
These other settings are essential to make things as crisp as possible.
On the sprite:
Ensure your sprites are using lossless compression e.g. True Color
Turn off mipmapping
Use Point sampling
In Render Quality Settings:
Turn off anisotropic filtering
Turn off anti aliasing
Turn on pixel snapping in the sprite shader by creating a custom material that uses the Sprite/Default shader and attaching it to the SpriteRenderer.
Also, I'd just like to point out that Unless you are applying Physics, Never Use FixedUpdate. Also, if your sprite has a Collider and is moving, it should have a Kinematic RigidBody attached even if you're never going to use physics, to tell the engine that the Collider is going to move.
Same problem here. I noticed that the camera settings and scale are also rather important to fix the rippling problem.
Here is What Worked for me:
Go to Project Settings > Quality
Under Quality Make the default Quality as High for all.
Set the Anistropic Texture to "Disabled"
Done, And the issue is resolved for me.
Image Reference:
enter image description here

Generating/building map from file

using a file I want to create a map and I am wondering about the best approach doing so.
Actually I searched the forum but I only found map generation algorithms that randomly creates maps.
Let's look at a minimal example.
e.g. Ihave a file containing
0110
1001
1000
0000
Every 0 shall be water and every 1 shall be earth.
I would handle this by simply havin two different bitmaps and loading them at the right coordinates. That'd be simple.
But let's guess we have a 1000*1000 big map and there is only enough space for 16*16 tiles per frame. Then I'd get the current position and would build the map around it.
Assuming we can only display 3*3 tiles, using the minimal example and being at position (2,2) where x and y is element 1..4 so what the user could see at this time would be:
011
100
100
Solution
I thought about using a text file, where a line represents the x-coordinate direction and
a column represents the y-coordinate direction. The whole file is being loaded at the beginning of the program. This shouldn't use too much ram assuming 1 tile needs 1 byte, what should be enough.
For redrawing the map when the user is moving, I'd get the moving direction and slide the current Bitmap for the height/width of a tile in the opposite direction and only look up the bitmaps for the new blank spaces. So I only need to look up the tile information for m+n-1 (where m is the amount of displayed tiles in y and n in x direction) tiles (max case if moving diagonal) instead of loading m*n tiles everytime the user moves.
Example
I created an example to make the above given example more easily to understand.
this is the whole map:
We can only display 3*3 tiles and the user is at position (2,2) so what we'd actually see is:
now he is moving towards the bottom right corner:
and the black framed section is being move to the opposite direction, so that we get:
now the blank tiles (black framed white areas) have to be looked up and teh final result will be:
Question
is this a good way of building a map? Or are there much faster functions, maybe already implemented in the microsoft xna-gamestudio package ?
I would pre-fetch 1-2 tiles range outside the screen view, so that you won't have weird pop-up as the player move.
But if your game is a top-down tile game, this solution is quite conservative. In most hardware today, you could create a very big range around the player without problem. Just look at the number of block Minecraft can process and display. Since you are reusing the same texture, you just load the asset once and reuse them in a tile, which would probably an object with very little memory footprint.
Have you tried implementing it yet?

2D Tile Lighting

I'm adding lighting to my XNA 2D tile based game.
I found this article useful, but the way its done it does not support collision. What I'd like is a method to do the following
Have always lit point
Collision (If the light ray hits a block, then dim the next block by whatever amount until its dark to simulate shadows)
I've been searching around for quite a while but no luck (I did find Catalin's tutorial, but it seemed a bit advanced for me, and didn't apply to tiles well due to redrawing the entire game for each point)
I'll share my method for applying a smooth lighting effect to a 2D tile grid. ClassicThunder's answer provides a nice link for shadows.
First off, we will need to calculate the lighting values of each tile which will be blurred later. Let me illustrate how this works before I get into the code.
Basicly what we do is loop through all the tiles, starting from the top, if a tile is blank, set the CurrentLight variable to max brightness, if we find a solid tile, set it as the CurrentLight variable and subtract an "absorbsion" amount from the CurrentLight. This way, on our next solid tile, when we set the tile to the CurrentLight value, it will be slightly less. This process is repeated until the array is iterated.
Now there will be a nice top to bottom lighting effect, but it isn't that great. We must repeat this process 3 more times, for bottom to top, left to right, and right to left. And it can be repeated more times for better quality.
Basically running this code on every tile in the loop
if (tile.Light > CurrentLight) //If this tile is brighter than the last, set the current light to the tiles light
CurrentLightR = tile.Light;
else if (CurrentLight != 0f) //If it is less, and isnt dark, then set the tile to the current light
tile.Light = CurLightR;
if (tile.Light == CurLightR) //If it is the same, subtract absorb values
CurrentLight -= tile.Absorb;
And there you go, nice tile lighting. However if you want a less "pixelized" look, you can check out my question on gamedev for that.
For per-pixel lighting, you might have to look somewhere else, because I don't know about that.
For per-tile lighting,
in SpriteBatch.draw, a few of the overloaded methods takes a color. When you use Color.White, the sprite that the SpriteBatch draws is normal colored.
Use Color multiplication by creating a new Color(Color.yourcolor.r*float, Color.yourcolor.y*float, Color.yourcolor.z*float, 255)
Basically, to get the float, try to find out a formula that calculates the brightness of the block due to nearby lights (stored in an array or list, probably). Since there's no normals needed for 2D games, this formula should be relatively easy.

Detect Rotation of a scanned image in C#

We want a c# solution to correct the scanned image because it is rotated. To solve this problem we must detect the rotation angle first and then rotate the image. This was our first thought for our problem. But then we thought image warping would be more accurate as I think it would make the scanned image like our template. Then we can process it as we know all the coordinates of our template... I searched for a free SDK or a free solution in c#. Helping me in this will be great as it is the last task in our work. Really, thanks for all.
We used the PrimeOCR product to do this. It's not free, but we couldn't find a free program that was comparable.
So, the hard part is to detect the angle of the page.
If you have full control over the template, the simplest way to do this is probably to come up with an easily-detectable symbol (e.g. a solid black circle) and stick 3 of them on the template. Then, detect them (just look for big blocks of pixels with high saturation, in the case of a solid black circle).
So, you'll then have 3 sets of coordinates. If you have a top circle, a left circle, and a right circle with all 3 circles at difference distances from one another, detecting which circle is the top circle should be pretty easy.
Then just call a rotation function. This part is easy and has been done before (e.g. http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-rotate ).
Edit:
I suggested a circle because it's easier to find the center, but a rectangle should work, too.
To be more explicit about how to actually locate the rectangles/circles, take the average Brightness value of every a × a group of pixels. If that value is greater than b, then that a × a group of pixels is part of a rectangle. a and b are varables you'll want to come up with yourself.
Use flood-fill (or, more precisely, Connected Component Labeling) group the resulting pixels together. The end result should give you your rectangles.

Categories